簡體   English   中英

如何驗證 Java 中的用戶輸入

[英]How do I validate user input in Java

我寫了一個 bmi 計算器程序,我想驗證用戶輸入,這樣用戶就不會為身高或體重輸入輸入負數。

我該怎么做呢? 我是 Java 的新手,所以我不知道。

import java.util.Scanner;
 
public class BMICalculator {
 
    public static void main(String[] args) throws Exception {
        calculateBMI();
    }
 
    private static void calculateBMI() throws Exception {
        System.out.print("Please enter your weight in kg: ");
        Scanner s = new Scanner(System.in);
        float weight = s.nextFloat();
        System.out.print("Please enter your height in cm: ");
        float height = s.nextFloat();
         
        float bmi = (100*100*weight)/(height*height);
         
        System.out.println("Your BMI is: "+bmi);
        printBMICategory(bmi);

        s.close();
       
    }
     

    private static void printBMICategory(float bmi) {
        if(bmi < 24) {
            System.out.println("You are underweight");
        }else if (bmi < 29) {
            System.out.println("You are healthy");
        }else if (bmi < 34) {
            System.out.println("You are overweight");
        }else {
            System.out.println("You are OBESE");
        }
    }
}

你可以一直要求價值,直到用戶輸入一個有效的數字

private float readZeroOrPositiveFloat(Scanner scanner , String promptMessage) 
{
     float value = -1;
     while(value < 0){
         System.out.println(promptMessage);
         value = scanner.nextFloat();
     }
     return value;
}


private static void calculateBMI() throws Exception {
    System.out.print("Please enter your weight in kg: ");
    Scanner s = new Scanner(System.in);
    float weight = readZeroOrPositiveFloat(s , "Please enter your weight in kg: ");
    float height = readZeroOrPositiveFloat(s , "Please enter your height in cm: ");
     
    float bmi = (100*100*weight)/(height*height);
     
    System.out.println("Your BMI is: "+bmi);
    printBMICategory(bmi);

    s.close();
   
}

只是為了處理negative輸入並不斷要求有效輸入,

boolean flag = true;
while(flag){
    System.out.print("Please enter your weight in kg: ");
    int weight = sc.nextInt();
    System.out.print("Please enter your height in cm: ");
    int height = sc.nextInt();
    if(weight >= 0 && height >= 0){
        float bmi = (100*100*weight)/(height*height);
        flag = false;
    }else{
        System.out.println("Invalid Input");
    }
}

要處理所有unexpected輸入並繼續要求有效輸入,

boolean flag = true;
    while(flag){
        Scanner sc = new Scanner(System.in);
        try{
            System.out.print("Please enter your weight in kg: ");
            int weight = sc.nextInt();
            System.out.print("Please enter your height in cm: ");
            int height = sc.nextInt();
            if(weight >= 0 && height >= 0){
                float bmi = (100*100*weight)/(height*height);
                flag = false;
            }else{
                System.out.println("Invalid Input");
            }
        }catch(Exception e){
            System.out.println("Invalid Input");
        }
    }

當您從掃描儀獲得輸入時,您可以使用 if 語句來確保該值不是負數。 例如:

if(input value <= 0){
    System.out.println("Invalid Input");
}else { *program continues* }

暫無
暫無

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

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