簡體   English   中英

輸入字母字符時退出Java程序

[英]quit java program when an alphabetic character is entered

我必須在Java中制作一個體重指數計算器進行分配,並且如果用戶在要求體重時輸入字母字符,我希望程序退出並顯示一條消息。 我需要創建if語句的幫助。 這是我所擁有的(不起作用)。 我嘗試了其他各種不平等符號,但均未成功。 我以為我可能必須使用if(在命令之間),但是我沒有發現一種使weight變量起作用的方法。

我收到的錯誤如下Lab4.java:21:如果(weight <= Z)^,錯誤找不到符號

 public class Lab4
    {
            public static void main (String[] args)
            {
                    Scanner kb = new Scanner (System.in);
                    System.out.println("enter e for english or m for metric");
                    String choice;
                    choice = kb.nextLine();
                    char firstchar;
                    firstchar = choice.charAt(0);
            boolean english;
            english = firstchar == 'e';
            if (english)
            {
                    // prompt the user to input their weight in pounds
                    System.out.println("Enter your weight in pounds");
                    double weight = kb.nextDouble();
                    if (weight <= Z)
                    {
                            System.out.println("letters are not an acceptable weight");
                            System.exit(0);
                    }
                    // prompt the user to input their height in inches
                    System.out.println("Enter your height in inches");
                    double height = kb.nextDouble();
                    if (height == 0.0)
                    {
                            System.out.println("0 is not a valid height");
                            System.exit(0);
                    }
                    // make  bmi to an integer and compute bmi by dividing weight by height^2 * 750
                    int bmi = (int)(weight/(height*height)*703);
                    // have the computer display height, wieght, and bmi
                    System.out.printf("your weight is %8.2f", weight);
                    System.out.println(" pounds");
                    System.out.printf("your height is %8.2f", height);
                    System.out.println(" inches");
                    System.out.println("your BMI is " + bmi);
                    if (bmi <= 24)
                            System.out.println("Normal Bodyweight");
                            else
                            {
                                    if (bmi > 30)
                                            System.out.println("Obese");
                                    else
                                            System.out.println("Overweight");

如果weight是輸入字符串。 您可以從以下位置使用isAlpha函數: https ://stackoverflow.com/a/5238524/1926621

然后將if (isAlpha(weight) )作為檢查輸入是否為字母順序的條件。

在您的代碼中,您正在使用kb.nextDouble()weight讀取為double ,這將引發InputMismatchException並且即使在執行/輸入if檢查(即weight <= Z )之前,程序也會終止。

另外,更重要的一點是, 您不能直接將doublechar類型進行比較 ,例如, weight <= Z

所以,讀weight使用作為字符串scanner.nextLine()如下圖所示,然后檢查是否如下圖所示它不包含英文字母(使用正則表達式):

String weight = kb.nextLine();
if(!weight.contains(/^\d*\.?\d*$/)) {
    System.out.println("letters are not an acceptable weight");
    System.exit(0);
}

考慮到weight為字符串-

if(!weight.matches("[-+]?\\d*\\.?\\d+")){
   System.out.println("Ikke akseptert. Quitter");
   System.exit(0);
} 

暫無
暫無

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

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