2016-09-22 9 views
-1

私はdoステートメントを使用して、人が得点を述べるようにしていますが、doステートメントのネストされたif文は正確に増分していないようです(つまり、 )?私が間違って書いているのは、正しい変数を増やしていないのですか?それを削除し、正しい結果が表示されます -do while文の一部が正しくカウントされませんでしたか?

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(正しい<= 3);ループが問題です。 '0%'行を出力するときに 'correct'の値を出力してみてください。 –

+1

問題は 'correct == 4'までループしていることです。これは' else'につながります。しかし、あなたがなぜ完全にループしているのかは明らかではありません。 – shmosel

答えて

1

次のように

は、ここでの主な問題は、あなたが実際にDO-whileループを必要としないことである私のコードです。

do-whileで起こっていることは、正しい変数が4以上の値になるまで繰り返していることです。 (質問が正しく答えられなかった場合は、無限ループが発生することを意味します。)ロジックが結果を出力すると、値が4以上の正しい場合は、if条件がトリガーされず、最後にelseが実行されます。 0%を表示します。

コードを単純化して改善するために、他にも数多くの方法があります。あなたは単に言語を学んでいると仮定します。

関連する問題