第28行的输入代码不起作用,出了什么问题?当我运行时,它一直运行到第28行然后程序退出。该程序编译没有错误。该程序询问用户他们的年龄,性别,名字和姓氏,如果他们结婚,他们是否超过20岁。如果用户不满20岁,该计划将不会询问他们是否已婚。
import java.util.Scanner;
class Gender {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
//asking the users age, name, and gender
System.out.print("What is your gender (M or F): ");
String gender = Input.nextLine();
System.out.print("First name: ");
String FirstName = Input.nextLine();
System.out.print("Last name: ");
String LastName = Input.nextLine();
System.out.print("Age: ");
int Age = Input.nextInt();
if (Age >= 20) {
System.out.print("Are you married " + FirstName + " (Y or N): ");
String AreYouMarried = Input.nextLine(); // << PROBLEM
if (AreYouMarried == "Y") {
if(gender == "M") {
System.out.println("Then I shal call you Mr." + FirstName + " " + LastName + ".");
}
else if(gender == "F") {
System.out.println("Then I shal call you Ms." + FirstName + " " + LastName + ".");
}
}
}
if(Age < 20) {
System.out.print("Then I shall call you " + FirstName + " " + LastName + ".");
}
}
}
答案 0 :(得分:3)
这不是比较Java中字符串的正确方法:
AreYouMarried == "Y"
使用以下内容:
"Y".equals(AreYouMarried)
同样适用于性别比较。
请勿在同一程序中混合调用nextInt
和nextLine
这会导致更多问题。坚持只使用其中一种
单个程序中的方法。
答案 1 :(得分:1)
这是因为nextInt()无法读取你按下的最后一个字符,因为ENTER =&#39; \ n&#39;。 要解决它,只需在询问婚姻之前调用另一个nextLine()。它会是这样的..
class Gender {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
//asking the users age, name, and gender
System.out.print("What is your gender (M or F): ");
String gender = Input.nextLine();
System.out.print("First name: ");
String FirstName = Input.nextLine();
System.out.print("Last name: ");
String LastName = Input.nextLine();
System.out.print("Age: ");
int Age = Input.nextInt();
if (Age >= 20) {
System.out.print("Are you married " + FirstName + " (Y or N): ");
Input.nextLine();
String AreYouMarried = Input.nextLine(); // << PROBLEM
//HERE CONTINUE CODING
并且不要忘记改变&#34; ==&#34;到.equals()。 你不能比较那样的字符串。
答案 2 :(得分:0)
将""
替换为equals()
方法,使用equals()
方法比较字符串内容相等
和
在Input.nextLine();
Syso("Are you married " + FirstName + " (Y or N): ");
public static void main(String[] args){
try {
Scanner Input = new Scanner(System.in);
//asking the users age, name, and gender
System.out.print("What is your gender (M or F): ");
String gender = Input.nextLine();
System.out.print("First name: ");
String FirstName = Input.nextLine();
System.out.print("Last name: ");
String LastName = Input.nextLine();
System.out.print("Age: ");
int Age = Input.nextInt();
if(Age >= 20){
System.out.print("Are you married " + FirstName + " (Y or N): ");
Input.nextLine();
String AreYouMarried = Input.nextLine();
if(AreYouMarried.equals("Y")){
if(gender.equals("M") ){
System.out.println("Then I shal call you Mr." + FirstName + " " + LastName + ".");
}else if(gender.equals("F") ){
System.out.println("Then I shal call you Ms." + FirstName + " " + LastName + ".");
}
}else{
System.out.println("XYZ msg.");
}
}
if(Age < 20){
System.out.print("Then I shall call you " + FirstName + " " + LastName + ".");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}