2016-09-22 20 views
-1

我正在嘗試使用do while語句(在此需要)陳述某人獲得的分數,但是在我的do語句中嵌套的if語句似乎沒有遞增正確(即無論如何它都會給出0% )?我錯誤地寫了什麼是不增加我的正確變量?做的一部分,而陳述不增加我的計數正確嗎?

這裏是我的代碼如下

Scanner input = new Scanner(System.in); 
int q1 = 2, //answer 1 correct answer 
q2 = 1, //answer 2 correct answer 
q3 = 3, //answer 3 correct answer 
correct = 0, //amount correct 
answer1, //store answer1 
answer2, //store answer2 
answer3; //store answer 3 

System.out.printf("%n Question #1.) What is the capital of Iowa?"); //display question 
System.out.printf("%n 1.) Iowa City \t 2.) Des Moines \t 3.) Dubuque"); //display answer 
System.out.printf("%n Your answer: "); 
answer1 = input.nextInt(); //input becomes initial value 

while (answer1 < 1 || answer1 > 3) //while loop to check if answer is valid 
{ 
System.out.printf("%n Invalid Answer please enter a valid answer: "); //ask for input if not valid 
answer1 = input.nextInt(); 
} 

switch (answer1) //switch statement for input types of answer1 
{ 
case 1: 
    System.out.printf("%n Incorrect"); //if input 1 it is incorrect 
    break; //go to next part of code 

case 2: 
    System.out.printf("%n Correct"); //if input 2 it is correct 
    break; 

case 3: 
    System.out.printf("%n Incorrect"); //if input 3 it is incorrect 
    break; 
} 


System.out.printf("%n Question #2.) Where is Iowa?"); //display question 
System.out.printf("%n 1.) Midwest \t 2.) South \t 3.) Northeast"); //display answer 
System.out.printf("%n Your answer: "); 
answer2 = input.nextInt(); //input becomes initial value 

while (answer2 < 1 || answer2 > 3) //make sure answer is valid 
{ 
System.out.printf("%n Invalid Answer please enter a valid answer: "); //ask for valid input 
answer2 = input.nextInt(); 
} 

switch (answer2) //switch statement for chosen answers 
{ 
case 1: 
    System.out.printf("%n Correct"); //correct if input is 1 
    break; 

case 2: 
    System.out.printf("%n Incorrect"); //incorrect for answer 2 
    break; 

case 3: 
    System.out.printf("%n Incorrect"); //incorrect for answer 3 
    break; 
} 

System.out.printf("%n Question #3.) Who is the new president of UIowa?"); //display question 
System.out.printf("%n 1.) Sally Mason \t 2.) Barack Obama \t 3.) Bruce Harreld"); //display answer 
System.out.printf("%n Your answer: "); 
answer3 = input.nextInt(); //input becomes initial value 

while (answer3 < 1 || answer3 > 3) //make sure valid answer 
{ 
System.out.printf("%n Invalid Answer please enter a valid answer: "); //ask for valid answer 
answer3 = input.nextInt(); 
} 

switch (answer3) //switch to give output depending on answer 
{ 
case 1: 
    System.out.printf("%n Incorrect"); //if input is 1 
    break; 

case 2: 
    System.out.printf("%n Incorrect"); //if input is 2 this statement 
    break; 

case 3: 
    System.out.printf("%n Correct"); //if input is 3 this statement 
    break; 
} 

do { 
if (answer1 == q1) { 
    correct++; 
} 
if (answer2 == q2) { 
    correct++; 
} 
if (answer3 == q3) { 
    correct++; 
} 
} while (correct <= 3); 
if (correct == 3) { 
System.out.printf("%n 100%%"); 
} else if (correct == 2) { 
System.out.printf("%n 66.67%%"); 
} else if (correct == 1) { 
System.out.printf("%n 33.33%%"); 
} else { 
System.out.printf("%n 0%%"); 
} 
} //end method 
} //end class 
+0

用調試器調試你的代碼可能比創建這個問題花費的時間要少。 –

+2

'do {...} while(correct <= 3);'loop是你的問題。打印出'0%'行時,請嘗試打印出'correct'的值。 –

+1

問題是你正在循環,直到'correct == 4',這會將你帶到'else'。但目前還不清楚爲什麼你要完全循環。 – shmosel

回答

1

的主要問題是,你實際上並不需要的do-while循環 - 刪除它,正確的結果會顯示出來。

發生了什麼事情,而你繼續循環,直到正確的變量值爲4或更多。 (也就是說,如果沒有問題被正確回答,你會有一個無限循環。)當你的邏輯去打印結果時,糾正值爲4或更大意味着沒有觸發if條件,導致最終的其他執行並顯示0%。

還有很多其他方法可以簡化和改進你的代碼 - 我假設你只是在學習語言。