【发布时间】:2022-08-17 01:29:45
【问题描述】:
为什么输入数字后我的if语句没有执行
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(\"enter 1 for convert time, enter 2 for digitssum: \");
String inputVal = sc.nextLine(); // Read user input
//System.out.println(\"Input is: \" + inputVal);
if (inputVal == \"1\")
{
System.out.println(\"enter a number to convert to time \");
String inputVal2 = sc.nextLine();
int inputVal3 = Integer.parseInt(inputVal2);
System.out.println(\"Res is: \" + digitsSum(inputVal3));
}
else if (inputVal == \"2\")
{
System.out.println(\"enter a number to digitsum \");
String inputVal2 = sc.nextLine();
int inputVal3 = Integer.parseInt(inputVal2);
System.out.println(\"Res is: \" + convertTime(inputVal3));
}
// Ask the user which game to play.
// Then ask the user for input and pass the value to the corresponding method.
// If the user enters 1, ask for an integer to convert and call the convertTime method.
// If the user enters 2, ask for an integer and call the digitsSum method.
// TODO: Your code goes here
sc.close();
System.out.println(\"test\");
}
}
-
对于字符串比较,您应该使用
inputVal.equals(\"1\")而不是inputVal == \"1\"
标签: java