只是尋求一點幫助!我目前正在做一個體重轉換程式。我讓它正常作業,但現在我試圖讓它萬無一失,即如果用戶輸入的數值低于或超過某個范圍(在這種情況下,我正在尋找 0 到 450 范圍之間的 KG)將出現一條提示錯誤的訊息,然后提示用戶再次輸入他們的值。我可以使用以下代碼做到這一點,但問題是當用戶輸入有效值時,它不僅會列印有效輸入的轉換,還會列印先前不正確值的轉換。我附上了命令提示符的螢屏截圖,演示了這個問題。有人可以告訴我哪里出錯了嗎?謝謝。
public void kgToStonesAndPounds()
{
double kg = 0;
System.out.println("Please enter weight in KG here, range must be between 1 and 450: ");
kg = input.nextDouble();
if ( kg >= 1 && kg <= 450 ) // validate kg
System.out.printf("\nThe weight you have entered is %.0f KG\n" , kg);
else
{System.out.println( "Weight in KG must be in the range of 1 - 450" );
this.kgToStonesAndPounds();
}
double pounds = kg * 2.204622;
double stonesWithDecimal = pounds / 14;
int stone = (int) stonesWithDecimal; // cast int to get rid of the decimal
long poundsWithoutStone = (long)((stonesWithDecimal - stone) * 14); // Take the fractional remainder and multiply by 14
System.out.println("This converts to " stone " Stone " poundsWithoutStone " Pounds " );
}//end method kgToStonesAndPounds
命令提示符示例
uj5u.com熱心網友回復:
return在無效情況下再次呼叫該方法后,您必須添加一個。這樣,當從方法呼叫回傳時,如果從該方法呼叫它,它將不會移出 else 陳述句并執行以下代碼。
public void kgToStonesAndPounds() {
...
if ( kg >= 1 && kg <= 450 ) // validate kg
System.out.printf("\nThe weight you have entered is %.0f KG\n" , kg);
else {
System.out.println( "Weight in KG must be in the range of 1 - 450");
this.kgToStonesAndPounds();
return; // here
}
...
}
uj5u.com熱心網友回復:
Java - 如何正確驗證一定范圍內的數字用戶輸入
只要您獲得所需的效果/結果(沒有不良的副作用),一種方法就是correct另一種。
這是我可以做到的。最初只提示,如果輸入不正確,則重復提示。
double kg;
String prompt = "Please enter weight in KG here, range must be between 1 and 450: ";
System.out.println(prompt);
while ((kg = input.nextDouble()) < 1 || kg > 450) {
System.out.println(prompt);
}
System.out.printf("\nThe weight you have entered is %.0f KG\n" , kg);
// now do something with kg
uj5u.com熱心網友回復:
遞回(呼叫自身內部的方法)不是處理錯誤的方法,它應該只在邏輯需要時使用。
要再次詢問,請使用僅在輸入有效時才退出的回圈
double kg;
do {
System.out.println("Please enter weight in KG here, range must be between 1 and 450: ");
kg = input.nextDouble();
if (kg >= 1 && kg <= 450) {
System.out.printf("\nThe weight you have entered is %.0f KG\n", kg);
break;
} else {
System.out.println("Weight in KG must be in the range of 1 - 450");
}
} while (true);
你可以使用模數來簡化你的代碼
double pounds = kg * 2.204622;
int stone = (int) pounds / 14;
int poundsWithoutStone = (int) pounds % 14;
System.out.println("This converts to " stone " Stone " poundsWithoutStone " Pounds ");
uj5u.com熱心網友回復:
Ausgefuchster 和 azro 的答案都有效,我將我的答案作為附加答案進行討論。我認為您的大部分代碼都可以正常作業,但您應該更清楚地構建代碼。因為 if 陳述句和 else 陳述句沒有共同的代碼來執行,所以方法中的所有代碼都應該分開到不同的分支中。如下所示:
public void kgToStonesAndPounds()
{
double kg = 0;
System.out.println("Please enter weight in KG here, range must be between 1 and 450: ");
kg = input.nextDouble();
if ( kg >= 1 && kg <= 450 ){ // validate kg
System.out.printf("\nThe weight you have entered is %.0f KG\n" , kg);
double pounds = kg * 2.204622;
double stonesWithDecimal = pounds / 14;
int stone = (int) stonesWithDecimal; // cast int to get rid of the decimal
long poundsWithoutStone = (long)((stonesWithDecimal - stone) * 14); // Take the fractional remainder and multiply by 14
System.out.println("This converts to " stone " Stone " poundsWithoutStone " Pounds " );
}
else
{System.out.println( "Weight in KG must be in the range of 1 - 450" );
this.kgToStonesAndPounds();
}
}//end method kgToStonesAndPounds
導致這個問題的原因是在 kgToStonesAndPounds 的遞回執行完成后,代碼將繼續運行 else 塊后面的其余代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/463712.html
