0

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");
        
        
    
}
    
        

}

}

Pedro
  • 9
  • 4
  • Move all the code in `main` into its own function then call that function in a loop. – 001 Sep 21 '22 at 20:05
  • This kind of programming against cases of user error are (to me) a waste of time. To start with, HTML 5 is now the vanguard of handling bad user input, and (IMO) this is what you should be learning on the topic of bad user input. Scanner is rarely used by professional Java programmers. My advice: "Fast fail". Let programs die quickly, and create a user manual in a text/word/etc file that highlights how to use the program correctly - it's a better way to go about it – ControlAltDel Sep 21 '22 at 20:15
  • I changed the code into a new file and then called it from the main. But i don't know how to call that function in a loop i get the error that Operations(the name i gave to the method), cannot be resolved to a variable. – Pedro Sep 21 '22 at 20:22
  • I guess so but i was asked to do it this way by the java professor and i need to send it to him working. – Pedro Sep 21 '22 at 20:23

0 Answers0