gpt4 book ai didi

Java - If语句逻辑错误

转载 作者:行者123 更新时间:2023-12-01 11:07:41 24 4
gpt4 key购买 nike

事件的顺序应该是:

1) I enter two.
2) I am prompted to choose a name.
3) I choose either Washington/ Franklin/ Hamilton.
4) I'm asked which denomination does this name appear on.
5) I give the answer.

但是,当我进入华盛顿参加第三部分时 - 我被告知这是一个无效号码。我不明白为什么会这样。

public static void main(String[] args) {
// TODO code application logic here
System.out.println("Type 1 to enter a denomination, 2 to enter a last name");
Scanner in = new Scanner(System.in);
int x = in.nextInt();

if(x==1){
System.out.println("Choose a denomination");
int y = in.nextInt();
in.nextLine();
if(y==1){
System.out.println("Which person appears on the 1 bill?");
String answer = in.nextLine();
if(answer.equals("Washington")){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}


else if(y==10){
System.out.println("Which person appears on the 10 bill?");
String answer = in.nextLine();
if(answer.equals("Hamilton")){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else if(y==100){
System.out.println("Which person appears on the 100 bill?");
String answer = in.nextLine();
if(answer.equals("Franklin")){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else{
System.out.println("That is an invalid number.");
}
}
else if(x==2){
System.out.println("Choose a name");
String y = in.nextLine();
in.nextLine();
if(y.equals("Washington")){
System.out.println("Which denomination does this name appear on?");
int answer = in.nextInt();
if(answer==1){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}


else if(y.equals("Hamilton")){
System.out.println("Which denomination does this name appear on");
int answer = in.nextInt();
if(answer==10){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else if(y.equals("Franklin")){
System.out.println("Which denomination does this name appear on");
int answer = in.nextInt();
if(answer==100){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else{
System.out.println("That is an invalid number.");
}
}
}

问题出在 x==2 段上。 x==1 工作正常。

最佳答案

问题在于看似额外的in.nextLine()

我建议您将所有 in.nextInt() 更改为 in.nextLine() 然后将它们解析为实际情况类型如 int

示例:

int answer = Integer.parseInt(in.nextLine());

建议更改的原因:当您使用 nextInt() 时,下一行仍然会停留在那里,直到您执行额外的 nextLine( ) 清除它。

为了防止此类问题,建议使用 nextLine() 接收所有输入,然后将其解析为实际类型(intdouble ..等)。

这也是 Microsoft 处理 C# 中整数输入的方式。

<小时/>

编辑后的工作代码:

public static void main(String[] args) {

System.out.println("Type 1 to enter a denomination, 2 to enter a last name");
Scanner in = new Scanner(System.in);
int x = Integer.parseInt(in.nextLine());

if(x==1){
System.out.println("Choose a denomination");
int y = Integer.parseInt(in.nextLine());
in.nextLine();
if(y==1){
System.out.println("Which person appears on the 1 bill?");
String answer = in.nextLine();
if(answer.equals("Washington")){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}


else if(y==10){
System.out.println("Which person appears on the 10 bill?");
String answer = in.nextLine();
if(answer.equals("Hamilton")){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else if(y==100){
System.out.println("Which person appears on the 100 bill?");
String answer = in.nextLine();
if(answer.equals("Franklin")){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else{
System.out.println("That is an invalid number.");
}
}
else if(x==2){
System.out.println("Choose a name");
String y = in.nextLine();
//in.nextLine();
if(y.equals("Washington")){
System.out.println("Which denomination does this name appear on?");
int answer = Integer.parseInt(in.nextLine());
if(answer==1){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}


else if(y.equals("Hamilton")){
System.out.println("Which denomination does this name appear on");
int answer = Integer.parseInt(in.nextLine());
if(answer==10){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else if(y.equals("Franklin")){
System.out.println("Which denomination does this name appear on");
int answer = Integer.parseInt(in.nextLine());
if(answer==100){
System.out.println("That is correct");
}
else{
System.out.println("That is incorrect");
}
}
else{
System.out.println("That is an invalid number.");
}
}
}

关于Java - If语句逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769001/

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