-1

I'm trying to figure out how to update my number in a method even though I predefined a static value before it and aim to update it inside the method. Any help would be greatly appreciated. I know that I set result to 0 but my question is how would I update it?

import javax.swing.*;

public class Arithmetic2
{
    public static void main(String args[])
    {
        double numberOne, numberTwo;
        String numberOneString, numberTwoString;
        String operation;
        double result;

        numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
        numberOne = Double.parseDouble(numberOneString);
        numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
        numberTwo = Double.parseDouble(numberTwoString);
        operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");

        // Call performOperation method here
        result = 0;
        performOperation(numberOne, numberTwo, operation, result);

        System.out.format("%.2f",numberOne);
        System.out.print(" " + operation + " ");
        System.out.format("%.2f", numberTwo);
        System.out.print(" = ");
        System.out.format("%.2f", result);

        System.exit(0);

    } // End of main() method.

    // Write performOperation method here.
    private static double result;

    public static void performOperation(double numberOne, double numberTwo, String operation, double result) {
        System.out.println("Res Equals the default 0: " + result);

        switch(operation) {
            case "+" : result = numberOne+numberTwo; break;
            case "-" : result = numberOne-numberTwo; break;
            case "*" : result = numberOne*numberTwo; break;
            case "/" : result = numberOne/numberTwo; break;
            case "%" : result = numberOne%numberTwo; break;
            default : System.out.println("Invalid operator!");
        }
        System.out.println("Result New: " + result);
    }
}

1 Answers1

-1

Your method performOperation returns a void value. That's an empty value. You have to return the value type of result which is double in your case.

So, change your current:

public static void performOperation(double numberOne, double numberTwo, String operation, double result) {
    System.out.println("Res Equals the default 0: " + result);

    switch(operation) {
        case "+" : result = numberOne+numberTwo; break;
        case "-" : result = numberOne-numberTwo; break;
        case "*" : result = numberOne*numberTwo; break;
        case "/" : result = numberOne/numberTwo; break;
        case "%" : result = numberOne%numberTwo; break;
        default : System.out.println("Invalid operator!");
    }
    System.out.println("Result New: " + result);
}

to this one:

public static double performOperation(double numberOne, double numberTwo, String operation) {
    double result = 0;
    System.out.println("Res Equals the default 0: " + result);

    switch(operation) {
        case "+" : result = numberOne+numberTwo; break;
        case "-" : result = numberOne-numberTwo; break;
        case "*" : result = numberOne*numberTwo; break;
        case "/" : result = numberOne/numberTwo; break;
        case "%" : result = numberOne%numberTwo; break;
        default : System.out.println("Invalid operator!");
    }
    System.out.println("Result New: " + result);
    return result;
}

And then change your code from this:

result = 0;
performOperation(numberOne, numberTwo, operation, result);

to this:

result = performOperation(numberOne, numberTwo, operation);
Socrates
  • 8,724
  • 25
  • 66
  • 113