[英]I'm trying to create an error message if the data entered in an array is less than zero, input validation
This method allows the user to input the rainfall for every month of the year. 该方法允许用户输入一年中每个月的降雨量。 I'm trying to prevent data less than zero from being stored in the array.
我试图阻止数据小于零存储在数组中。 I'm using a do-while loop, but I can't seem to figure out how to check if the input is less than zero.
我正在使用do-while循环,但我似乎无法弄清楚如何检查输入是否小于零。 Thanks for your help guys, cheers!
谢谢你的帮助,欢呼!
public static double[] getRainFall()
{
double[] rainfallMonths = new double[12];
double[] rainfall = new double[12];
do
{
for(int x = 0; x < rainfallMonths.length; x++)
{
System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
rainfallMonths[x] = keyboard.nextDouble();
rainfall[x] = rainfallMonths[x];
if(rainfallMonths < 0)
{
System.out.println("Input is Invalid");
}
}
}while(rainfallMonths < 0);
for(int count = 0; count < rainfallMonths.length; count++)
{
System.out.println("Rainfall Month #" + (count + 1) + ": " + rainfall[count]);
}
return rainfall;
}
Your logic is a little off, not to mention that you're trying to compare an array to an int
... 你的逻辑有点偏,更不用说你试图将数组与
int
进行比较......
First, the logic... 一,逻辑......
do
for x = 0 to rainfallMonths.length -1 do
... get input...
while value < 0
The problem here is, you've already assigned the input to all the elements of the array in the for-next
loop, but then you are trying to validate the value that was input outside of the for-next
which is likely never to return a valid result...and it's too late... 这里的问题是,你已经在
for-next
循环中为数组的所有元素分配了输入,但是你试图验证在for-next
之外输入的值,这可能永远不会返回一个有效的结果......现在为时已晚......
Instead, you want to reverse the logic... 相反,你想要扭转逻辑......
for x = 0 to rainfallMonths.length -1 do
do
value = get input from user
while value < 0
rainfallMonths[x] = value
Next, rainfallMonths
is a reference to an array, this isn't actually what you want to be checking against, you need to be checking against one it's values or elements, for example... 接下来,
rainfallMonths
是对数组的引用,这实际上并不是你想要检查的,你需要检查一个它的值或元素,例如......
while (rainfallMonths[x] < 0);
And if none of that made sense... 如果这些都没有意义......
public static double[] getRainFall()
{
double[] rainfallMonths = new double[12];
double[] rainfall = new double[12];
for(int x = 0; x < rainfallMonths.length; x++)
{
double input = 0;
System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
do {
rainfallMonths[x] = keyboard.nextDouble();
rainfall[x] = rainfallMonths[x];
if(input < 0)
{
System.out.println("Input is Invalid");
}
} while (rainfallMonths[x] < 0);
}
for(int count = 0; count < rainfallMonths.length; count++)
{
System.out.println("Rainfall Month #" + (count + 1) + ": " + rainfall[count]);
}
return rainfall;
}
You might want to take a refresher on Arrays which should help ;) 你可能想对数组进行复习,这应该有所帮助;)
double temp = -1;
for(int x = 0; x < rainfallMonths.length; x++)
{
System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
temp = keyboard.nextDouble();
if(temp < 0)
{
System.out.println("Input is Invalid");
x--; //if you want the user to maybe try to repeat this month again?
}
else
{
rainfallMonths[x] = keyboard.nextDouble();
rainfall[x] = rainfallMonths[x];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.