我試圖創建一條錯誤消息,該方法允許用戶輸入一年中每個月的降雨量。我試圖阻止數據存儲在數組中。我正在使用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;
}
'如果(rainfallMonths [X] <0)'...但你會想改變周圍的這點你循環... – MadProgrammer
'rainfallMonths'只是一個參考。你可能想檢查一下'rainFallMonths [index]' –
你想在rainMonths <0的時候結束循環,而且你已經弄錯了你的代碼。參見@MadProgrammer的上面評論。 – Jaykishan