Home > other >  Why does my calculator program does not work as it should?
Why does my calculator program does not work as it should?

Time:10-31

    import java.util.Scanner;   

    public class calculator {
    static double sum, operand;
    static char op;
    static char answer;
    public static void main(String[] args) {
    run_calc();
    }
   

        
    public static void run_calc() {
        
        while(answer!='q' && answer !='Q')  {
        System.out.println("Enter operator  and optional operand to calculate. Enter 'q' to exit the program.");
        scandata(op, operand);
        do_next_op( op,operand, sum);
        System.out.println("The result so far is"   sum);
        }
        
    
    }
    public static void scandata(char op, double operand) {
        Scanner input = new Scanner(System.in);
         op = input.nextLine().charAt(0);
    
       if(op ==' ' || op == '-' || op == '*' || op =='/' || op =='^' ) {
        operand = input.nextDouble();
       }
    }
    
public static void do_next_op(char op, double operand, double sum)
{
    switch(op)
    {
        case ' ': sum  = operand; break;
        case '-': sum -= operand; break;
        case '*': sum *= operand; break;
        case '/': sum = (operand == 0) ? sum  : sum / operand; break;
        case 'q': System.out.println(" The final value of the accumulator is"   sum);   default: break;
    }
}
}

this is my code just a simple a calculator. My problems are 1.When I insert q to exit the program, it just keeps running. 2.When i insert 2 it should show " the result so far is 2 " but it just shows 2

I spend almost an hour to debug it but I still don't know whats wrong with it. Can someone tell me whats wrong with it?

CodePudding user response:

I modified your code some. Here are the results of one of my tests.

Enter operator and optional operand to calculate. Enter 'q' to exit the program.
 8
The result so far is 8.0
Enter operator and optional operand to calculate. Enter 'q' to exit the program.
-3
The result so far is 5.0
Enter operator and optional operand to calculate. Enter 'q' to exit the program.
*10
The result so far is 50.0
Enter operator and optional operand to calculate. Enter 'q' to exit the program.
/5
The result so far is 10.0
Enter operator and optional operand to calculate. Enter 'q' to exit the program.
q
The final value of the accumulator is 10.0

A method takes in zero or more parameters and returns either nothing (void) or a single value. That value can be an array or a class instance, where the class holds multiple values.

You create one Scanner instance for System.in, and pass that instance to whichever method needs to read input from the console.

Class names begin with an upper case letter. Method names begin with a lower case letter. Method names generally are a verb-noun combination.

Here's the complete runnable code.

import java.util.Scanner;

public class SimpleCalculator {

    public static void main(String[] args) {
        runCalculator();
    }

    public static void runCalculator() {
        Scanner input = new Scanner(System.in);
        char op;
        double sum = 0.0;
        
        do {
            String line = readInput(input);
            op = line.charAt(0);
            double operand = 0.0;
            if (line.length() > 1) {
                operand = Double.valueOf(line.substring(1));
            }
            sum = processOperation(op, operand, sum);
            writeOutput(op, sum);
        } while (op != 'q' && op != 'Q');
        
        input.close();
    }
    
    public static String readInput(Scanner input) {
        System.out.println("Enter operator and optional operand to "
                  "calculate. Enter 'q' to exit the program.");
        return input.nextLine().trim();
    }
    
    public static double processOperation(char op, double operand, double sum) {
        switch (op) {
        case ' ':
            sum  = operand;
            break;
        case '-':
            sum -= operand;
            break;
        case '*':
            sum *= operand;
            break;
        case '/':
            sum = (operand == 0) ? sum : sum / operand;
            break;          
        default:
            break;
        }
        
        return sum;
    }
    
    public static void writeOutput(char op, double sum) {
        if (op != 'q' && op != 'Q') {
            System.out.println("The result so far is "   sum);
        } else {
            System.out.println("The final value of the accumulator is "   sum);
        }
    }

}

CodePudding user response:

The problem you have is called "shadowing".

When you name your function arguments with the same names you gave your class variables, the parameters hide the class variables from any code in that function.

Function parameters are just like local variables - when the function exits, they cease to exist.

So, op and operand in your scandata function, are different then op and operand in your other functions, and will never have the same values.

You loose the values you read from the user as soon as you exit scandata.

To preserve the values, remove the function arguments, or better yet - learn how to return a value from a function!

  •  Tags:  
  • java
  • Related