How to show a question again when a wrong answer is entered?
I wrote the code below for my homework. It should ask the user to calculate two things and it should stop when null is entered. It works, except from one thing and that is that it should show the same question again if you give the wrong answer. What do I have to do to show the same question again when I enter for example 24 to question 1?
----------------------------------------------------------
Code Java:
import java.util.Scanner;
public class SmallMathematicsApplication{
public static void main(String[] args) {
int som1;
int som2;
int reportError;
Scanner reader = new Scanner(System.in);
System.out.print("5+20? ");
som1 = reader.nextInt();
if (som1 == 25) {
System.out.println("good job");}
else if (som1 == 0) {
System.out.println("you stopped the program by entering null");System.exit(-1);}
else {
System.out.println("try it again");}
System.out.print("5+26? ");
som2 = reader.nextInt();
if (som2 == 31) {
System.out.println("good job, you finished the exercise");}
else if (som2 == 0) {
System.out.println("you stopped the program by entering null");System.exit(-1);}
else {
System.out.println("try it again");}
}}
Re: How to show a question again when a wrong answer is entered?
Sounds like a job for a do while loop to me.
Also, your bracket placement is really wonky: I suggest putting your closing brackets on their own line. I almost thought you were asking why your if statements weren't working.
Re: How to show a question again when a wrong answer is entered?
Works now, thanks a lot. I just started learning Java, have to work on the brackets.