Java 如何获得一个返回1到54的方法,不包括参数中传递的数字?

Java 如何获得一个返回1到54的方法,不包括参数中传递的数字?,java,methods,arguments,Java,Methods,Arguments,所以我现在被要求做一个项目,写一个方法,返回一个从1到54的随机数,不包括参数中传递的数字 公共类随机数选择器{ 公共静态void main(字符串[]args){ System.out.println(getRandom(新的int[]{1,2,3,4,5,6,7,8,9,10, 11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30, 31,32,33,34,35,36,37,38,39,40,})); } 公共静态整数ge

所以我现在被要求做一个项目,写一个方法,返回一个从1到54的随机数,不包括参数中传递的数字

公共类随机数选择器{
公共静态void main(字符串[]args){
System.out.println(getRandom(新的int[]{1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,}));
}
公共静态整数getRandom(整数…数字){
长时间;
int结果;
时间=System.currentTimeMillis();
结果=(int)(时间%54)+1;
for(int i=0;i

但是问题仍然存在,因为从该方法生成的数字仍然被传递,并且没有被排除。

我建议您首先编写一个方法来检查该数字是否在数组中(
包含),然后调用它。。。也许是这样的

// check if the array numbers contains the element n.
public static boolean contains(int[] numbers, int n) {
  if (numbers != null && numbers.length > 0) {
    for (int i : numbers) {
      if (i == n) {
        return true;
      }
    }
  }
  return false;
} // end of contains

public static int getRandom(int... numbers) {
  long time = System.currentTimeMillis();
  for (;;) { // loop forever.
    int result = (int) (time % 54) + 1;
    if (!contains(numbers, result)) { // until the result ISN'T within numbers.
      return result;
    }
  }
} // end of getRandom

所以我看到的问题是,结果只能推导一次。因此,如果搜索包含结果,您的方法仍将返回它。这是你可以考虑的事情清单

  • 使用while循环
  • 每次迭代获得一个新的
    结果
  • 使用
    for
    循环检查该结果是否在数组中
  • 如果结果在数组中,则继续循环
  • 如果未找到,则返回
    结果
因此,最终您的方法结构可以像这样循环。我会给你一个布局。你可以试着自己解决剩下的问题

 public static int getRandom(int... numbers) {
     long time;
     int result;

     while (true) {
         time = ...
         result = ...

         boolean found = false;

         for (... ; ... ; ... ) {
             if ( ... ) {           // if you find it
                 found = true;
                 break;
             }
         }

         if (!found) {         // if the result is not found in the     
             return result;    // array, return it.
         }                   
     }
     return -1;  // dummy value to return, since a method must return something
 }               // Note: this method should never return -1 though. It will
                 // continue looping until the number is found

你自己都做了些什么?为什么你认为这里的人只是你的仆人?!请在此处发布代码,不要发布代码链接。尽管@AmirPashazadeh非常粗鲁,非常没有建设性,但他说得对,您需要向我们提供您的代码,告诉我们你自己做了什么来解决它,然后我们可以帮助你。很抱歉,我是Stackoverflow新手。这个网站有一个非常好的内置代码显示,所以使用它,而不是重定向我们,它是一样容易把它放在这里,因为它是把它放在那里,除非它指导我们到一个工作的例子。
 public static int getRandom(int... numbers) {
     long time;
     int result;

     while (true) {
         time = ...
         result = ...

         boolean found = false;

         for (... ; ... ; ... ) {
             if ( ... ) {           // if you find it
                 found = true;
                 break;
             }
         }

         if (!found) {         // if the result is not found in the     
             return result;    // array, return it.
         }                   
     }
     return -1;  // dummy value to return, since a method must return something
 }               // Note: this method should never return -1 though. It will
                 // continue looping until the number is found