這是我的代碼,它給用戶隨機方程來測試他們的知識,並且沒有錯誤出現。當我運行代碼時,它只是不能在Eclipse中編譯。當我執行intelliJ Idea時,它會運行,但沒有代碼出現,我什麼也做不了。如何讓我的java代碼編譯?
package logical;
import java.util.Random;
import java.util.Scanner;
public class logical {
@SuppressWarnings("resource")
public static void main (String args[]){
int qn1, qn2; //Numbers for the question
int ua; //The users answer
int answer; //The actual answer
int counter = 0; //Controls the number of questions that can be answered
int tally = 0; //Number of correct responses
String again = "y";
Scanner in = new Scanner(System.in);
Random dice = new Random();
while(again == ("y") || again == ("Y")){
for(counter=1;counter>=5;counter++){
qn1 = 1+dice.nextInt(40);
qn2 = 1+dice.nextInt(40);
answer = qn1 + qn2;
System.out.printf("What is %d + %d",qn1,qn2);
ua = in.nextInt();
if(ua == answer){
System.out.println("That is correct!");
counter++;
tally++;
}
else{
System.out.println("Sorry, that is wrong! The correct answer is " + answer);
}
System.out.println("Would you like to try again?(Y/N)");
again = in.next();
if(again=="y" || again=="Y"){
counter=0;
}else {
System.out.println("Thanks for playing your results were " + tally + " out of 5");
}
}
}
System.out.println("Thanks for taking the math challenge!");
}
}
和您的編譯錯誤消息是.....? –
這裏你有邏輯錯誤'again ==(「y」)'你需要使用'equals()'方法比較字符串,而不是'==' –
請刪除花括號上那些醜陋的評論。我不在乎你在大學教過什麼,這使得閱讀更難。 – byxor