如何在java中退出返回循环?

我对编程本身并不陌生,我已经研究了C#已经有一段时间了,但我自己并没有真正做过很多练习,我现在刚开始使用java,因为我想做Android应用程序和测试我新获得的知识我想做一个基本的控制台计算器,这是我到目前为止所得到的:

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

我知道它可能不是最好或更有效的计算器,但正如我所说的,我刚开始使用java,这是我目前所知的全部,程序可行,因为我可以添加,分割或我选择的其他任何东西,但在那之后,我希望它给我选择一个不同的操作,我在返回后把“choice = null”,但它似乎没有工作,我试过到目前为止有几件事,但我开始认为我可能误解了实际上有什么回报,所以我认为最好转向你们寻求帮助.

任何建议表示赞赏,谢谢!

解决方法:

代替

return adition(result, result);

在你的adition方法中,返回结果.

否则你只是重复相同的方法,直到堆栈溢出. (同样适用于其他方法).

你的while(选择!= null){loop是没有必要的,因为你总是设置choice = null;.既然您也没有在该循环中更改选择的值,您也可以删除循环.

将参数传递给adition(etc)方法没有意义,因为您总是会覆盖它们.只需在这些方法中将它们声明为局部变量:

public static float adition() {  // No parameters
    // ...
    float n1 = input.nextFloat();
    // ... 
    float n2 = input.nextFloat();
上一篇:java-使用JButton将文本添加到两个文本字段


下一篇:python 模块import(26)