簡體   English   中英

我正在嘗試創建一個錯誤消息,如果在數組中輸入的數據小於零,輸入驗證

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM