簡體   English   中英

Java - 如何正確驗證特定范圍內的數字用戶輸入

[英]Java - How to Validate Numerical User Input Within A Certain Range Correctly

只是尋求一點幫助。 我目前正在研究一個重量轉換程序,我讓它工作正常,但現在我正試圖讓它萬無一失。 也就是說,如果用戶輸入的數值低於或超過某個范圍(在這種情況下,我正在尋找 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

命令提示符中的示例

在無效的情況下再次調用該方法后,您必須添加一個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
    }
    
    ...
}

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

遞歸(在自身內部調用方法)不是處理錯誤的方法,它應該只在邏輯需要時使用。

要再次詢問,請使用僅在輸入有效時才會退出的循環

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 ");

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塊后面的rest代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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