【问题标题】:In java, How would I check input validation for an array used to input grades (can't be negative, can't be over 100)在 java 中,如何检查用于输入成绩的数组的输入验证(不能为负数,不能超过 100)
【发布时间】:2012-10-05 17:31:29
【问题描述】:

到目前为止,我已经尝试创建一个 while 循环,该循环仅在输入的数组值是“正确”的值(0 到 100 之间)时才会退出。 Wich 意味着如果输入的值是负数或像字符这样的随机值,则循环应该重复。

到目前为止,我的代码只有在输入的所有成绩都不正确时才有效。如果我输入 0、100 和 -2,它仍然会通过,即使 -2 应该会导致循环重复。我需要修改什么以仅允许将 0 到 100 之间的值输入到数组中?

到目前为止的代码:

//Input validation for grades
int g = 0;

while(g >= 0)
{
  System.out.print("Please Enter the Students' Grades: ");
  for (int c = 0; c < studentGrades.length; c++)
  {
    studentGrades[c] = input2.nextInt();

    if (studentGrades[c] >= 0 && studentGrades[c] <= 100)
    {
       g = -1;
    }
  }
}

【问题讨论】:

  • 您能否更具体地说明问题发生在哪里?

标签: java arrays validation loops


【解决方案1】:

在这段代码中sn-p:

studentGrades[c] >= 0 && studentGrades[c] <= 100

...你是说如果输入的等级大于等于0且小于等于100,将g设置为-1(并退出循环)。

因此,基本上,每次输入有效成绩时,您的循环都会退出。你做了与你想要的相反的事情。尝试在您的条件之前添加! 来否定它。

【讨论】:

    【解决方案2】:

    应该这样做

         outer:
            while(g >= 0) {
                System.out.print("Please Enter the Students' Grades: ");
    
                for (int c = 0; c < studentGrades.length; c++) {
                    studentGrades[c] = input2.nextInt();
                    System.out.println(" input is "+studentGrades[c]);
                    if (studentGrades[c] <= 0 || studentGrades[c] >= 100) {
                        break outer;
                    }
                }
            }
    

    【讨论】:

      【解决方案3】:

      在将下一个添加到数组之前进行检查。如果超出范围,请要求另一个值。

      【讨论】:

        【解决方案4】:

        您需要翻转逻辑,目前您的g 标志以“无效”开头(意味着如果g 没有改变,则循环继续),当您找到有效成绩时,您标记@987654323 @ 有效。

        相反,在while 循环的顶部将g 设置为-1,然后如果提供的任何成绩无效,则将其设置回0,例如:

        int g = 0;
        while(g >= 0) {
            g = -1;
            System.out.print("Please Enter the Students' Grades: ");
            for (int c = 0; c < studentGrades.length; c++) {
                studentGrades[c] = input2.nextInt();
                if (studentGrades[c] < 0 || studentGrades[c] > 100) {
                    g = 0;
                }
            }
        }
        

        【讨论】:

          【解决方案5】:

          我认为这是你想要达到的(你也可以增强它)

          int[] values = new int[5]; //modify this according to your size
          int index = 0;
          do {
              int value;
              do {
                  System.out.println("Enter value " + (index + 1) + ": ");                
                  value = input.nextInt();
              } while (value < 0 || value > 100); //make sure each value is valid
              values[index++] = value; //add value to the array
          } while(index < values.length); //make sure you get all the values
          

          如果不需要 for 循环,那么我认为这样看起来更好。

          【讨论】:

            【解决方案6】:

            将您的while loop 移动到for loop 中,因为我猜您想为每个index 验证您的input 等级:-

            你的条件应该是g &lt; 0。此外,您在 if 中使用了反向条件。您正在设置 g = -1 以获得正确的输入。您需要在if 中更改您的条件。

            for (int c = 0; c < studentGrades.length; c++) {
            
                g = -1; // Set to invalid to get your while run first time.
            
                while(g < 0) {
                    System.out.print("Please Enter the Students' Grades: ");
                    studentGrades[c] = input2.nextInt();
            
                    // Check invalid grade -> negative or more than 100
                    if (studentGrades[c] < 0 || studentGrades[c] > 100) {
                        g = -1;
            
                    } else {
                        // Break while if correct grade has been entered. Continue with next index
                        break;  
                    }
                }
            }
            

            【讨论】:

              【解决方案7】:

              无论如何,这段代码看起来都会循环直到它获得studentGrades.length 值,如果while 循环重复,它将永远重复,因为g 没有重新初始化。

              我认为这一定是一个家庭作业问题,所以我将把修复错误的实际编码留给你作为练习。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-12-20
                • 1970-01-01
                • 2017-12-17
                • 1970-01-01
                • 1970-01-01
                • 2014-03-16
                • 1970-01-01
                相关资源
                最近更新 更多