如何使用String和Scanner修复开关盒

时间:2018-12-28 02:28:25

标签: java string switch-statement

输入性别(M或F)后,程序将立即结束,而无需进入切换状态

我尝试将'string'更改为“ string”,但什么都没有改变。我认为那会有所作为

import java.util.Scanner;
public class FemurHumerus {

  public static void main(String[] args) {

    String gender;
    double FemHum;
    double MaleHum;
    double FemFem;
    double MaleFem;
    int FemHumR;
    int MaleHumR;
    int FemFemR;
    int MaleFemR;

    Scanner scan = new Scanner(System.in);

    System.out.println("What is your gender? (M or F): ");
    gender = scan.nextLine();

    switch (gender) {

        case "F":


            System.out.print("Enter femur length: ");
            FemFem = in.nextInt();
            FemFemR = (int) (FemFem*1.94+28.7);

            System.out.print("Enter humerus length: ");
            FemHum = in.nextInt();
            FemHumR = (int) (FemHum*2.8+28.2);


            System.out.println("Height based on femur: "+FemFemR);
            System.out.println("Height based on humerus: "+FemHumR);

            break;

        case "M":

            System.out.print("Enter femur length: ");
            MaleFem = in.nextInt();
            MaleFemR = (int) (MaleFem*1.88+32);

            System.out.print("Enter humerus length: ");
            MaleHum = in.nextInt();
            MaleHumR = (int) (MaleHum*2.9+27.9);


            System.out.println("Height based on femur: "+MaleFemR);
            System.out.println("Height based on humerus: "+MaleHumR);

            break;

        default:

            System.out.println("Gender not sprecified.");

            break;
    }

  }

}

预期输出应为基于股骨和肱骨的特定性别的身高

2 个答案:

答案 0 :(得分:1)

我认为由于in.nextInt();,导致无法解决的编译错误
将所有in.nextInt();更改为scan.nextInt();即可。

答案 1 :(得分:1)

我同意;将您的in.nextInt();更改为scan.nextInt(),除非声明您的身份,并且您没有显示其目的。

enter image description here

然后,您将获得开关盒。