【发布时间】:2015-01-09 18:24:35
【问题描述】:
第 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 + ".");
}
}
}
【问题讨论】:
-
“不会工作”是什么意思,哪个是28?
-
欢迎来到 SO。请指出问题区域。 20号线是哪个??
-
程序一到达代码的那部分就停止工作。
-
@JamesNelson 代码的哪一部分?
-
@JamesNelson 它不会停止工作,只是你的字符串
==比较(以你编码的方式)可能都会失败。