我參加了高中的一門編程課程,我的老師拒絕教我們並用簡單的答案回答問題。 (他們都非常模糊)我們總是讓項目過去的時間,只需要爲自己謀生。我們這周的項目是輸入五個數字並使用for循環(不是數組),而是輸入的數字是偶數或奇數。這是我到目前爲止的代碼,我在一個類中爲我的一位朋友提供了「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.");
}
}
}
}
}
就像一個建議一樣,當詢問有關學生作業的問題時,不給出完成的解決方案通常是一個好主意,而是指導說明或參與討論。這讓學生自己找到答案,而你是他們學習經歷的一部分。 –
我試過這段代碼,它不會退出編譯階段,(對於我的原始代碼,這是同樣的問題)有什麼想法嗎? – radianthellion
什麼是編譯錯誤,你認爲它們是什麼意思? –