How can i get this code to ask if you want to do another operation? And ask again for numbers and an operation if the asnwer is yes. I think it should be with and if like if the operation works fine it should ask if you want to try again and if you say yes it should ask again, and if you say no it stops.
public class Interface {
public static int promptInt( Scanner sc, String prompt )
{
System.out.print( prompt );
while( ! sc.hasNextInt() )
{
System.out.println( "Invalid input, must be an integer" );
System.out.print( prompt );
sc.next(); //if the input is not a number throw away invalid input
}
return sc.nextInt();
}
public void suma(int j, int k) {
}
public static void main(String[] args) {
try {
Integer number1;
Integer number2;
char operator;
Scanner sc = new Scanner(System.in);
number1 = promptInt( sc, "Give me number 1: " );//Here we ask for the numbers
number2 = promptInt( sc, "Give me number 2: " );
System.out.println("Choose between +, /, - or *");
operator = sc.next().charAt(0);
int result = 0;
switch(operator) {//With this switch we go through the operations
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
result = number1 / number2;
if(number1 == 0 || number2 == 0 ) {
System.out.println("Don't divide by 0, please");//If we try to divide by 0 we get an error
}
break;
default:
System.out.println("We don't contemplate that operation");//If we pass something different from +,* or / it gives an error
}
System.out.println("The result of the " + " operation is " + result );
if(operator == '+' || operator == '/' || operator == '*' ) {
System.out.println("Do you want to try another operation?");
}
}catch (InputMismatchException e) {
System.out.println("You are not inserting a number, try again");
}
}
}