【问题标题】:Java Methods and ArraysJava 方法和数组
【发布时间】:2019-03-07 13:15:04
【问题描述】:

我刚开始学习 Java,我已经在互联网上搜索了几个小时,但似乎找不到可以帮助我完成任务的东西。我有一个数组,我需要为它编写一个方法。这似乎很容易,但我似乎无法将两者联系在一起。我了解这些方法,但我们没有将它们与数组一起使用,所以我完全糊涂了。如果这里有类似的答案,请指出正确的方向。

感谢您的宝贵时间。

问题:

编写一个方法,从用户那里接收一个介于 1 到 10 之间的整数,并确定该数字是否是随机生成的数组的一部分。它必须具有 (int []) 的方法签名并返回一个布尔值。

public class ArrayExample {

    public int [] createRandomArray() {
        int size = (int) (Math.random() * 10) + 1;
        int[] array = new int [size];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) (Math.random() * 10 ) + 1;
        }
        return array;
    }

    public static void main(String [] args) {       

    }
}

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 当您已经使用过方法时,那么您已经足够了解至少可以启动该方法。不管你是通过String strint i还是int[] intArray,都非常相似。
  • 我认为你应该看看for-loop。您可能希望遍历数组并比较每个元素是否等于用户输入。
  • 请注意,如果您不将其设置为 static(或初始化 ArrayExample 对象),您将无法在 main 中调用 createRandomArray 方法。
  • 描述究竟是什么让你感到困惑

标签: java


【解决方案1】:

如下所示:

public class ArrayExample {

public static int [] createRandomArray() {
    int size = (int) (Math.random() * 10) + 1;
    int[] array = new int [size];
    for (int i = 0; i < array.length; i++) {
        array[i] = (int) (Math.random() * 10 ) + 1;
    }
    return array;
}

private static boolean checkForNumInArray(int[] randomArrayInput){
  //your logic goes here
  // ask user for input number - Scanner/BufferedReader
  //search for that number in array - Loops
  // if found return true, otherwise return false - if-else
}

public static void main(String [] args) {       
    int[] randomArray = createRandomArray();
    boolean isPresent = checkForNumInArray(randomArray);
}
}

【讨论】:

  • 谢谢,这就是我要找的,我不知道如何设置它。我现在就开始工作。 :)
【解决方案2】:

你可以通过代码来了解

public class ArrayExample {

        public int [] createRandomArray() {
            int size = (int) (Math.random() * 10) + 1;
            int[] array = new int [size];
            for (int i = 0; i < array.length; i++) {
                array[i] = (int) (Math.random() * 10 ) + 1;
            }
            return array;
        }

        public int getUserInput() {
           //Take input from user and check it is between 1 and 10.
        }

        public boolean search(int[] arr, int input) {
            // Use some searching algorithm. Linear search will suit as the array is randomly generated.
            // if input is present in array return true else return false.
        }

        public static void main(String [] args) {  
           int input = getUserInput(); 
           boolean result = search(createRandomArray(), input);   
           //Print a message based on result.
        }
    }

【讨论】:

    【解决方案3】:

    在 main 方法中,您只需将整数循环从 1 迭代到 10 并检查它是否存在于您创建的数组中。

    public static void main(String[] args) { 
      int arr[] = createRandomArray(); 
      for(int i=0;i<=10;i++) { 
        if(Arrays.binarySearch(arr, i) == 0) { System.out.println("yes"); } 
      } 
    }
    

    【讨论】:

    • 没有。甚至达不到要求
    • public static void main(String[] args) { int arr[] = createRandomArray(); for(int i=0;i
    • 是的,我知道你的意思,但再说一遍:不。它甚至不能满足要求。这不仅仅是“它会起作用”,问题是“它会起作用并按照要求的方式设计”
    • 你需要一个排序数组来使用二分搜索,不是吗?
    • 是 @uneq95 ,我们使用 Arrays.sort 对数组进行排序
    猜你喜欢
    • 2013-12-31
    • 2015-07-11
    • 2012-03-07
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2013-11-29
    • 1970-01-01
    相关资源
    最近更新 更多