输入字母字符时退出java程序

时间:2017-04-13 22:19:54

标签: java

我必须在java中为分配制作一个体重指数计算器,如果用户在询问体重时输入字母字符,我希望程序退出并显示一条消息。我需要帮助创建if语句。这就是我所拥有的(没有工作)。我尝试过各种其他不等式符号但没有成功。我以为我可能不得不使用if(在命令之间),但我没有找到一种方法来使用权重变量。

我收到的错误如下 Lab4.java:21:错误找不到符号                          if(重量< = 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");

3 个答案:

答案 0 :(得分:1)

如果weight是输入字符串。您可以使用来自https://stackoverflow.com/a/5238524/1926621

的isAlpha函数

然后执行if (isAlpha(weight) )作为检查输入是否按字母顺序排列的条件。

答案 1 :(得分:1)

在您的代码中,您正在使用weight double阅读kb.nextDouble()InputMismatchException会抛出if,程序将在执行/输入{{1}之前终止检查(即weight <= Z)。

另外,更重要的一点是您无法直接将doublechar类型进行比较,例如weight <= Z

所以,使用weightscanner.nextLine()作为字符串读取,如下所示,然后检查它是否包含字母表(使用正则表达式),如下所示:

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

答案 2 :(得分:0)

考虑weight是字符串 -

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