我最近刚开始,这个网站曾帮我一次,我希望它能帮助我。
我知道我必须连接到其他地方,但我不知道如何,这是我的代码,
import java.util.Scanner;
public class even {
public static void main(String[] args) {
System.out.println("Enter a number.");
Scanner scan = new Scanner(System.in);
int n;
n = scan.nextInt();
boolean even;
even = n % 2 == 0;
if (even = true) {
System.out.println("Your number number is even");
else
System.out.println("Your number is odd");
}
}
}
我很抱歉,如果这个问题不合适,但我是新手并且在学习,请不要不喜欢它并建议下次做什么
答案 0 :(得分:1)
if (even == true) { //can be also written as if (even)
System.out.println("Your number number is even");
} else { //we enter the next block only when `even` is not true
System.out.println("Your number is odd");
}
有关if
语句的更多详细信息,here。
答案 1 :(得分:1)
关闭支架,类似
if (even) { // even = true is not good. even == true would work. but if (even)
// is shorter.
System.out.println("Your number number is even");
} else {
System.out.println("Your number is odd");
}
当您说if (even = true)
时,您要将true
分配给even
,而true
也评估为{{1}}。
答案 2 :(得分:-1)
如果/ Else语句通过使用花括号来编织在一起来控制代码和逻辑的流程。您的if语句应更改为:
if (even) {//even is true
System.out.println("Your number number is even");
} else {
System.out.println("Your number is odd");
}