【问题标题】:How can I specify what numbers should an array accept from a scanner如何指定数组应从扫描仪接受哪些数字
【发布时间】:2020-05-10 12:22:26
【问题描述】:
public static void main(String[] args) {
    Scanner input =  new Scanner(System.in);
    boolean isArrayInputStillGoing = true;
    while (isArrayInputStillGoing){
    System.out.println("Please enter the size of the array");
    int sizeOfArray = input.nextInt();
    if (sizeOfArray < 0){
        System.err.println("Array cannot have a negative value try again");
        continue;
    }
    int[] initialArray = new int[sizeOfArray];
    System.out.println("Please enter the array elements");
    for (int i=0;i<sizeOfArray;i++){
        initialArray[i] = input.nextInt();
        if (initialArray[i] <= 0 || initialArray[i] > 100){
            System.out.println("Try again with a number between 1 and 100");
            continue;
        }
    }
    isArrayInputStillGoing = false;
        renderArray(initialArray);
    }
}

我怎样才能做到这样当输入小于 1 或大于 100 的数字时,它要求您再次输入相同的数字。因为它只是打印出消息,然后打印带有任何输入数字的数组,即使它们是错误的。还有就是说 continue 是不必要的,因为它是循环的结尾。

【问题讨论】:

    标签: java arrays if-statement while-loop


    【解决方案1】:

    当错误的数字被发送到您继续的数组时,您将在数组中插入值,在该数组中 for 循环递增 i 计数器然后再次初始化新数组,只需在 if 块中写入 i-- 就可以了设置也删除继续

    public static void main(String[] args) {
            Scanner input =  new Scanner(System.in);
            boolean isArrayInputStillGoing = true;
            while (isArrayInputStillGoing){
            System.out.println("Please enter the size of the array");
            int sizeOfArray = input.nextInt();
            if (sizeOfArray < 0){
                System.err.println("Array cannot have a negative value try again");
                continue;
            }
            int[] initialArray = new int[sizeOfArray];
            System.out.println("Please enter the array elements");
            for (int i=0;i<sizeOfArray;i++){
                initialArray[i] = input.nextInt();
                if (initialArray[i] <= 0 || initialArray[i] > 100){
                    System.out.println("Try again with a number between 1 and 100");
                    i--;
    
                }
            }
            isArrayInputStillGoing = false;
            }
        }
    

    【讨论】:

      【解决方案2】:

      除了将input.nextInt() 分配给initialArray[i] = input.nextInt(); 中位置i 的数组之外,您还可以先将其分配给您要测试的某个候选人。您可以通过例如替换for循环如下:

            System.out.println("Please enter the array elements");
            int i = 0;
            while (i < sizeOfArray) {
      
              int candidate = input.nextInt();
      
              if (candidate <= 0 || candidate > 100){
                System.out.println("Try again with a number between 1 and 100");
              } else {
                initialArray[i] = candidate;
                i++;
              }
            }
      

      【讨论】:

        猜你喜欢
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多