简体   繁体   English

我正在尝试创建一个错误消息,如果在数组中输入的数据小于零,输入验证

[英]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.

相关问题 weka 3.8 中没有足够的 memory(堆上小于 50 m)错误消息 - Not enough memory (less than 50 m on heap) error message in weka 3.8 为数组中的数字小于20的数组创建JUnit,尝试创建假定的数组,然后进行测试 - Creating a JUnit for an array with a number less than 20 in the array, trying to create the assumed array, to then test 我正在尝试在 Eclipse 中运行 LWJGL 3 并且它一直显示此错误消息 - I'm trying to run LWJGL 3 in Eclipse and it keeps showing this error message 输入值少于数组长度 - Less input values than array length 如果原始类型和数组小于零,是否可以比较? - Is there a way to compare if a primitive type and an array are less than zero? 我正在尝试创建导航抽屉,但出现此错误 - I'm trying to create navigation drawer but I have this error 我正在尝试创建一个可以接受整数和数组的arrayList - I'm trying to create an arrayList that could accept both integers and array 我试图设置一个仅输入整数的要求,如果输入的其他任何内容都显示错误消息 - I am trying to set a requirement to only enter an integer and if anything else ins entered show error message 我正在尝试创建Dynamic EditText但是获取错误 - I'm trying to create Dynamic EditText But getting Error 在数组中查找少于M个下一个元素的元素 - Find an element in the array that is less than M next elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM