so im creating jeapordy in java, , dont care spelt wrong(if did) have 1 question coded far 1 answer, , asks question prints out wrong if answer right.
it asking first history question , answer george, printing out answer wrong. first history question worth 100. have not began code math part yet.
thanks if can fin problem! simple beginner.
import java.util.random; import java.util.scanner; public class game { public static void main (string[] args){ //utilites scanner s = new scanner(system.in); random r = new random(); //variables string[] mathquestions; mathquestions = new string[3]; mathquestions[0] = ("what sum of 2 + 2"); mathquestions[1] = ("what 100 * 0"); mathquestions[2] = ("what 5 + 5"); string[] historyquestions; historyquestions = new string[3]; historyquestions[0] = ("what general washingtons first name?"); historyquestions[1] = ("who won wwii, japan, or usa?"); historyquestions[2] = ("how many states in usa?"); //intro system.out.println("welome jeapordy!"); system.out.println("there 2 categories!\nmath , history"); system.out.println("math history"); system.out.println("100 100"); system.out.println("200 200"); system.out.println("300 300"); system.out.println("which category like?"); string categorychoice = s.nextline(); system.out.println("for how money?"); int moneychoice = s.nextint(); if (categorychoice.equalsignorecase("history")){ if (moneychoice == 100){ system.out.println(historyquestions[0]); string useranswer = s.nextline(); s.nextline(); if (useranswer.equalsignorecase("george")){ system.out.println("congratulations! right"); } else{ system.out.println("ah! wrong answer!"); } } } } }
when call nextint()
, newline character left unread, subsequent call nextline()
return empty string (since reads end of line). call newline()
once prior read/discard trailing newline:
if (moneychoice == 100) { system.out.println(historyquestions[0]); s.nextline(); // <-- string useranswer = s.nextline(); system.out.println(useranswer); ...
as aside, don't forget close scanner
when you're finished it: s.close()
.