[英]Need help. If and else statements both printing.
System.out.print("Please input 1 if you are a Women if not enter 2 if you are a man:");
int gender = input.nextInt();
if (gender == 1){
System.out.println("Woman") ;
}
else if(gender == 2);{
System.out.println("Man");}
When entering 1 it prints out Women and Man, but when entering 2 it prints out only Man. 输入1时,将打印出“女人和男人”,而输入2时,则仅打印出“男人”。 I am wondering why its doing that.
我想知道为什么要这么做。
Change 更改
else if(gender == 2);
to 至
else if(gender == 2)
The semicolon is treated as empty statement which gets executed when the else if condition is met. 分号被视为空语句,如果满足条件则在其他情况下执行。 Remove the semicolon and it will work fine.
删除分号,它将正常工作。
else if(gender == 2)
删除else if(gender == 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.