Java – How do I exit the return loop in Java?

How do I exit the return loop in Java?… here is a solution to the problem.

How do I exit the return loop in Java?

I’m no stranger to programming myself, I’ve been learning C# for a long time but I haven’t really done a lot of practice myself, I’m just starting to use java now because I want to do Android apps and test my newfound knowledge I want to make a basic console calculator, which is what I’ve gotten so far :

package calculatorSource;

import java.util.*;

public class calculatorJava {
    private static Scanner input;

public static void main(String[] args) {
        System.out.println("Select an operation");

String choice = null;
        input = new Scanner(System.in);

while(choice == null) {
            System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module.");
            choice = input.next();
        }

while(choice != null) {
            if(choice.equals("a")) {
                System.out.println(adition(0, 0));
                choice = null;
            } else if(choice.equals("s")) {
                System.out.println(subtraction(0, 0));
                choice = null;
            } else if(choice.equals("m")) {
                System.out.println(multiplication(0, 0));
                choice = null;
            } else if(choice.equals("d")) {
                System.out.println(division(0, 0));
                choice = null;
            } else if(choice.equals("mo")) {
                System.out.println(module(0, 0));
                choice = null;
            } else {
                System.out.println("Option not available, please try again.");
                choice = null;
            }
        }
    }

public static float adition(float n1, float n2) {
        input = new Scanner(System.in);

float result;

System.out.println("Enter first number:");
        n1 = input.nextFloat();

System.out.println("Enter second number:");
        n2 = input.nextFloat();

result = n1 + n2;

System.out.println("Result is: " + result);

return adition(result, result);
    }

public static float subtraction(float n1, float n2) {
        input = new Scanner(System.in);

float result;

System.out.println("Enter first number:");
        n1 = input.nextFloat();

System.out.println("Enter second number:");
        n2 = input.nextFloat();

result = n1 - n2;

System.out.println("Result is: " + result);

return subtraction(result, result);
    }

public static float multiplication(float n1, float n2) {
        input = new Scanner(System.in);

float result;

System.out.println("Enter first number:");
        n1 = input.nextFloat();

System.out.println("Enter second number:");
        n2 = input.nextFloat();

result = n1 * n2;

System.out.println("Result is: " + result);

return multiplication(result, result);
    }

public static float division(float n1, float n2) {
        input = new Scanner(System.in);

float result;

System.out.println("Enter first number:");
        n1 = input.nextFloat();

System.out.println("Enter second number:");
        n2 = input.nextFloat();

result = n1 / n2;

System.out.println("Result is: " + result);

return division(result, result);
    }

public static float module(float n1, float n2) {
        input = new Scanner(System.in);

float result;

System.out.println("Enter first number:");
        n1 = input.nextFloat();

System.out.println("Enter second number:");
        n2 = input.nextFloat();

result = n1 % n2;

System.out.println("Result is: " + result);

return module(result, result);
    }
}

I know it

may not be the best or more efficient calculator, but as I said, I just started using Java and that’s pretty much all I know so far, the program works because I can add, a department or whatever else I choose, but after that I want it to give me the option to choose a different operation, I put “choice = null” immediately after returning but it doesn’t seem to work, I’ve tried several things so far, But I’m starting to feel like I might be misunderstanding what return actually does, so I think it’s best to ask you guys for help.

Thanks for any suggestions, thank you!

Solution

Replace

return adition(result, result);

In your adition method, return result.

Otherwise, you will repeat the same method until the stack overflows. (The same goes for other methods).


Your while (choice != null) loop is not necessary because you always set choice = null; 。 Because you also don’t change the value of choice in the loop in any other way, you can also delete the loop.


It doesn’t make sense to pass parameters to the adition (etc.) method because you will always overwrite them. Just declare them as local variables in these methods:

public static float adition() {  // No parameters
    // ...
    float n1 = input.nextFloat();
    // ... 
    float n2 = input.nextFloat();

Related Problems and Solutions