gpt4 book ai didi

java - 如何使用 String 和 Scanner 修复 Switch-case

转载 作者:行者123 更新时间:2023-12-02 01:44:08 25 4
gpt4 key购买 nike

输入性别(男或女)后,程序立即结束,而不会进入开关盒

我尝试将“字符串”更改为“字符串”,但仍然没有任何变化。我认为这会有所作为

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;
}

}

}

预期输出应该是基于股骨和肱骨的特定性别的高度

最佳答案

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

enter image description here

然后你就会得到你的开关盒。

关于java - 如何使用 String 和 Scanner 修复 Switch-case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com