![](/img/trans.png)
[英]Not enough memory (less than 50 m on heap) error message in weka 3.8
[英]I'm trying to create an error message if the data entered in an array is less than zero, input validation
该方法允许用户输入一年中每个月的降雨量。 我试图阻止数据小于零存储在数组中。 我正在使用do-while循环,但我似乎无法弄清楚如何检查输入是否小于零。 谢谢你的帮助,欢呼!
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;
}
你的逻辑有点偏,更不用说你试图将数组与int
进行比较......
一,逻辑......
do
for x = 0 to rainfallMonths.length -1 do
... get input...
while value < 0
这里的问题是,你已经在for-next
循环中为数组的所有元素分配了输入,但是你试图验证在for-next
之外输入的值,这可能永远不会返回一个有效的结果......现在为时已晚......
相反,你想要扭转逻辑......
for x = 0 to rainfallMonths.length -1 do
do
value = get input from user
while value < 0
rainfallMonths[x] = value
接下来, rainfallMonths
是对数组的引用,这实际上并不是你想要检查的,你需要检查一个它的值或元素,例如......
while (rainfallMonths[x] < 0);
如果这些都没有意义......
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;
}
你可能想对数组进行复习,这应该有所帮助;)
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.