How can I get my loop to keep re-entering new variables and operators to continue to calculate?

I am attempting to have the user enter in multiple values and the computer would then return the answer. For instance they would enter in 10 + 11 - 15 / 12 ? then the it would output the answer. I have tested this and it only will take the first number and the last operator and the last number entered and calculate that answer. (output from about would of been 10/12) From what I can tell my math methods are working correctly, but I have attached them as well. How can I get it to continue to read in new user inputs then do the math to figure out the answer?

This is my code:

static void evaluate (){
    // this loads a scanner and prompts the user for input
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the expression: ");     
    int answer = 0; 
    int num = 0;
    int input1, input2;
    char operator = '+' ;
    input1 = input.nextInt();
    operator = input.next().charAt(0);
    input2 = input.nextInt(); 
    if(operator == '?')
        return;
    //this loop will continue until ? is entered
    do{
    if(operator == '?')
        break;
    operator = input.next().charAt(0);
    input2 = input.nextInt(); 
    // this will detect which operator is used 
    switch (operator) {
        case '+': answer = addition(input1, input2);
                  break;
        case '-': answer = subtract(input1, input2);
                  break;
        case '*': answer = multiply(input1, input2);
                  break;
        case '/': answer = division(input1, input2);
                  break;
        case '%': answer = remainder(input1, input2);
    } } while (operator != '?'); 
    System.out.println("The result is " + answer);  
}  

These are the methods for addition, subtraction, division, multiplication, and modulos.

// this is the math if a + is used as an operator
static int addition (int num1, int num2){
    int add;
    add = num1 + num2;
    return add;
}
// this is the math if a - is used as an operator
static int subtract (int num1, int num2){
    int minus;
    minus = num1 - num2;
    return minus;
}
// this is the math if a * is used as an operator
static int multiply (int num1, int num2){
    int multi;
    multi = num1 * num2;
    return multi;
}
// this is the math if a / is used as an operator
static int division (int num1, int num2){
    int div;
    div = num1 / num2;
    return div;
}
// this is the math if a % is used as an operator
static int remainder (int num1, int num2){
    int modulos;
    modulos = num1 % num2;
    return modulos;
}

Answers


Your logic is perfectly fine and all you have to do is put this condition if(operator == '?') break; after you read the value in operator by doing operator = input.next().charAt(0);.

One other thing is that you are never changing the value of input1. You should start assigning answer back to input1 after performing any operation.

Here is the modified code snippet:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter The Expression: ");     
    int answer = 0, num = 0, input1, input2;
    char operator = '+' ;

    /* Read Input 1 */
    input1 = input.nextInt();

    //this loop will continue until ? is entered
    do {
        /* Read Operator */
        operator = input.next().charAt(0);
        if(operator == '?') break;
        /* Read Input 2 */
        input2 = input.nextInt(); 
        // this will detect which operator is used 
        switch (operator) {
            case '+': answer = addition(input1, input2);
                      break;
            case '-': answer = subtract(input1, input2);
                      break;
            case '*': answer = multiply(input1, input2);
                      break;
            case '/': answer = division(input1, input2);
                      break;
            case '%': answer = remainder(input1, input2);
        }
        /* Input1 will be the answer now */
        input1 = answer;
    } while (operator != '?'); 
    System.out.println("Result: " + answer);   
}

// this is the math if a + is used as an operator
static int addition (int num1, int num2){
    int add;
    add = num1 + num2;
    return add;
}
// this is the math if a - is used as an operator
static int subtract (int num1, int num2){
    int minus;
    minus = num1 - num2;
    return minus;
}
// this is the math if a * is used as an operator
static int multiply (int num1, int num2){
    int multi;
    multi = num1 * num2;
    return multi;
}
// this is the math if a / is used as an operator
static int division (int num1, int num2){
    int div;
    div = num1 / num2;
    return div;
}
// this is the math if a % is used as an operator
static int remainder (int num1, int num2){
    int modulos;
    modulos = num1 % num2;
    return modulos;
}

Input:

10 + 11 - 15 / 2 ?

Output:

Result: 3

Also, I hope you are putting white spaces between your input numbers like what you have shown in the question above. Note that with the way you are reading input 10 + 11 - 15 / 12 ? will work fine but 10+11-15/12? will not.


Need Your Help

PDO class not found fatal error after upgrading to php 5.3.x

php mysql pdo

We had a server upgrade where we moved form 5.2.x to php 5.3.x. I was mysql pdo with the code showen below which was working before the upgrade.

copy a Matrix into another by memcpy in C

c matrix memcpy void-pointers

I want to write a function to copy a two matrices into one matrix by use of memcpy. It is like C = [A; B] in Matlab. Because, I want to use this function for all the type of data, which means integ...