making a simple math quiz with switch statement for loop and while loop in java -
so problem in code need ask 5 questions , ask user if he/she wants continue or stop. if choose continue should take them menu(switch statement) , can choose set of math problems. each option in switch statement should have 5 math questions. if choose stop code should count how many questions got right , calculate percantage correct. tips or wrong code.. know missing loop because i'm not quiet sure put , how make code work in.
{ //keyboard reader scanner in = new scanner((system.in)); //3 number variables int num1; int num2; int answer; int operator; int question = 0; int num3; double questioncount = 0; double correct = 0; int = 0; while (i < 7) { num1 = (int)(1+math.random()*10); num2 = (int)(1+math.random()*10); num3 = (int)(1+math.random()*100); system.out.println("welcome wsu school of math! "); system.out.println("please choose 1 of following options math quiz: "); system.out.println("1: addition number 1-10"); system.out.println("2: additon numbers 1-100"); system.out.println("3: subtraction numbers 1-10"); system.out.println("4: subtraction numbers 1-100"); system.out.println("5: multipication numbers 1-10"); system.out.println("6: exit quiz"); operator = in.nextint(); switch (operator) { case 1: system.out.println(num1+"+"+num2+"="); question = num1 + num2; break; case 2: system.out.println(num1+"+"+num3+"="); question = num1 + num3; break; case 3: system.out.println(num1+"-"+num2+"="); if(num1 < num2) { int temp = num1; num1 = num2; num2 = temp; } question = num1 - num2; break; case 4: system.out.println(num3+"-"+num1+"="); if(num1 > num3) { int temp = num1; num1 = num3; num3 = temp; } question = num1 - num3; break; case 5: system.out.println(num1+"*"+num2+"="); question = num1 * num2; break; case 6: if(operator == 6) { system.exit(-1); } break; } answer = integer.parseint(in.next()); if(answer == -99) { system.out.print("good bye!\n"); } else if (answer == question) { system.out.print("correct!\n"); questioncount++; correct++; } else { system.out.print("incorrect.\n"); system.out.println("the correct answer = "+question ); questioncount++; } } system.out.println("amount of problems attempted: "+questioncount); system.out.println("amount of problems correct: "+ correct); system.out.println("percent got on test = "+correct/questioncount*100); }
try write "case 6" this:
case 6: system.out.println("amount of problems attempted: "+questioncount); system.out.println("amount of problems correct: "+ correct); system.out.println("percent got on test = "+correct/questioncount*100); system.exit(-1); break; i cut "system.out.print ..." , put them inside "case 6:". system.exit(-1); close application. if want exit while loop need boolean inter loop , change value inside "case 6" this:
boolean = true; while(a){ //... case 6: //... = false; break; //... } or can make sure (i<7) become false. depends on want achieve. hope help!!!
Comments
Post a Comment