我在高中的编程2课程中,我的老师拒绝教我们并用直接的答案回答问题。 (他们都非常模糊)我们总是让项目超越时间,只需要自己照顾自己。我们本周的项目是输入五个数字并使用for循环(非数组)状态,而输入的数字是偶数或奇数。这是我到目前为止的代码,我为" for"我的朋友在课堂上排队,这可能是代码的问题。 (也就是说,当我尝试运行该程序时,它不会退出编译阶段)非常感谢你!
import java.util.*;
public class Project {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int one = scan.nextInt();
int two = scan.nextInt();
int three = scan.nextInt();
int four = scan.nextInt();
int five = scan.nextInt();
for (int i = 1; i < 5; i++) {
if (one < -1) {
System.out.println("An Error Has Ocurred.");
} else {
if (one % 2 == 0) {
System.out.println("This is an even number");
} else {
System.out.println("This is an odd number.");
}
}
if (two < -1) {
System.out.println("An Error Has Ocurred.");
} else {
if (two % 2 == 0) {
System.out.println("This is an even number");
} else {
System.out.println("This is an odd number.");
}
}
if (three < -1) {
System.out.println("An Error Has Ocurred.");
} else {
if (three % 2 == 0) {
System.out.println("This is an even number");
} else {
System.out.println("This is an odd number.");
}
}
if (four < -1) {
System.out.println("An Error Has Ocurred.");
} else {
if (four % 2 == 0) {
System.out.println("This is an even number");
} else {
System.out.println("This is an odd number.");
}
}
if (four < -1) {
System.out.println("An Error Has Ocurred.");
} else {
if (four % 2 == 0) {
System.out.println("This is an even number");
} else {
System.out.println("This is an odd number.");
}
}
if (five < -1) {
System.out.println("An Error Has Ocurred.");
} else {
if (five % 2 == 0) {
System.out.println("This is an even number");
} else {
System.out.println("This is an odd number.");
}
}
}
}
}
答案 0 :(得分:1)
我想你可以尝试一下。 `
import java.util.*;
public class Project {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
int number = scan.nextInt();
if ((number & 1) == 0) {
System.out.println(number + " is Even");
} else {
System.out.println(number + " is Odd");
}
}
}
}
输入为10 15 64 68 75
Output: 10 is Even 15 is Odd 64 is Even 68 is Even 75 is Odd
答案 1 :(得分:0)
首先,您应该将参数String[] args
添加到main method
。
java main方法如下所示:public static void main(String args[])
。
其次,您不需要使用此代码来执行您想要的操作。您只需要阅读the java doc并让研究人员精神奕奕。