简体   繁体   English

在java中,我如何检查用于输入成绩的数组的输入验证(不能为负数,不能超过100)

[英]In java, How would I check input validation for an array used to input grades (can't be negative, can't be over 100)

So far I've tried to create a while loop that only kicks out if the entered array value is a "correct" one (between 0 and 100). 到目前为止,我已经尝试创建一个while循环,只有在输入的数组值是“正确的”(0到100之间)时才会启动。 Wich means that the loop should repeat if the value entered is a negative number or something random like a char. 如果输入的值是负数或类似char的随机值,则意味着循环应该重复。

My code so far only works if all grades entered are incorrect. 到目前为止,我的代码只有在输入的所有成绩都不正确的情 If I enter a 0, 100, and -2 it still goes through, even though -2 should cause the loop to repeat. 如果我输入0,100和-2仍然会通过,即使-2应该导致循环重复。 What do I need to modify to only allow values that are between 0 and 100 to be entered into the array? 我需要修改哪些只允许将0到100之间的值输入到数组中?

Code so far: 代码到目前为止:

//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;
    }
  }
}

In this code snippet: 在此代码段中:

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

... you are saying that if the entered grade is greater than or equal to 0 and it is less than or equal to 100, set g to -1 (and exit the loop). ...你说如果输入的等级大于或等于0且小于或等于100,则将g设置为-1(并退出循环)。

So essentially, every time a valid grade is entered, your loop will exit. 基本上,每次输入有效等级时,您的循环都将退出。 You've done the opposite of what you wanted. 你完成了与你想要的相反的事情。 Try adding a ! 尝试添加一个! before your condition to negate it. 在你的条件之前否定它。

This should do 这应该做

     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;
                }
            }
        }

Move your while loop inside the for loop , because I guess you want to validate your input grade for each index : - for loop移动while loop ,因为我想你想验证每个index input等级: -

And your condition should be g < 0 . 你的病情应该是g < 0 Also, you have used reverse condition in your if. 此外,您在if中使用了反向条件。 You were setting g = -1 for correct input. 您正在设置g = -1以获得正确的输入。 You need to change your condition in if . 你需要改变你的条件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;  
        }
    }
}

Put a check before you add the next into the array. 在将下一个添加到数组之前进行检查。 If it's out of bounds, ask for another value. 如果它超出范围,请求另一个值。

You need to flip the logic around, currently your g flag starts as "invalid" (meaning that if g doesn't change, the loop continues), and when you find a valid grade you mark g as valid. 你需要翻转逻辑,目前你的g标志开始为“无效”(意味着如果g没有改变,循环继续),当你找到有效等级时,你将g标记为有效。

Instead, at the top of the while loop set g to -1 , and then set it back to 0 if any of the grades provided are invalid, for example: 相反,在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;
        }
    }
}

I think this what you are trying to achieve (also you can enhnace it) 我认为这是你想要达到的目标(也可以让你感到高兴)

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

If for loop is not required then I think it looks better that way. 如果不需要for循环那么我认为它看起来更好。

This code looks like it will loop until it gets studentGrades.length values, no matter what, and if the while loop repeats, it will repeat forever because g is not reinitialized. 这段代码看起来会循环,直到它获得studentGrades.length值,无论如何,如果while循环重复,它将永远重复,因为g没有重新初始化。

I assume this must be a homework problem, so I'll leave the actual coding to fix the bug to you as an exercise. 我认为这必须是一个家庭作业问题,所以我将留下实际编码来修复这个bug作为练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM