java - Count the correct score -


i given assignment asks questions , calculate total score @ end. here code. tia

    scanner userinput = new scanner(system.in);     int useranswer;      system.out.print("q1.) main character in legend of zelda?\n"             + "1.) link\n2.) princess zelda\n3.)super mario"             + "\nanswer: ");     useranswer = userinput.nextint();     if (useranswer == 1){         system.out.print("that correct! ");     }     else {         system.out.print("i'm sorry wrong.");     }      system.out.print("\nq2.) main character in naruto?\n"             + "1.) sasuke\n2.) naruto\n3.) sakura"             + "\nanswer: ");     useranswer = userinput.nextint();     if (useranswer == 2){         system.out.print("that correct! ");     }     else {         system.out.print("i'm sorry wrong.");     }      system.out.print("q3.) main character in dragon balls??\n"             + "1.) krillin\n2.) bulma\n3.) goku"             + "\nanswer: ");     useranswer = userinput.nextint();     if (useranswer == 3){         system.out.print("that correct! ");     }     else {         system.out.print("i'm sorry wrong.");     } 

the question is, statement should add add total scores?

you need variable hold score , increase each time answer correct :

scanner userinput = new scanner(system.in); int useranswer; int totalscore = 0;  system.out.print("q1.) main character in legend of zelda?\n"         + "1.) link\n2.) princess zelda\n3.)super mario"         + "\nanswer: "); useranswer = userinput.nextint(); if (useranswer == 1){     system.out.print("that correct! ");     totalscore = totalscore + 1;  // or totalscore++ } else {     system.out.print("i'm sorry wrong."); }  system.out.print("\nq2.) main character in naruto?\n"         + "1.) sasuke\n2.) naruto\n3.) sakura"         + "\nanswer: "); useranswer = userinput.nextint(); if (useranswer == 2){     system.out.print("that correct! ");     totalscore = totalscore + 1;  // or totalscore++ } else {     system.out.print("i'm sorry wrong."); }  system.out.print("q3.) main character in dragon balls??\n"         + "1.) krillin\n2.) bulma\n3.) goku"         + "\nanswer: "); useranswer = userinput.nextint(); if (useranswer == 3){     system.out.print("that correct! ");     totalscore = totalscore + 1;  // or totalscore++ } else {     system.out.print("i'm sorry wrong."); }  system.out.print("your total score " + totalscore); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -