电竞比分网-中国电竞赛事及体育赛事平台

分享

如何在java中退出返回循環(huán)?

 印度阿三17 2019-07-24

我對(duì)編程本身并不陌生,我已經(jīng)研究了C#已經(jīng)有一段時(shí)間了,但我自己并沒(méi)有真正做過(guò)很多練習(xí),我現(xiàn)在剛開(kāi)始使用java,因?yàn)槲蚁胱?a href="https:///tag/Android">Android應(yīng)用程序和測(cè)試我新獲得的知識(shí)我想做一個(gè)基本的控制臺(tái)計(jì)算器,這是我到目前為止所得到的:

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

我知道它可能不是最好或更有效的計(jì)算器,但正如我所說(shuō)的,我剛開(kāi)始使用java,這是我目前所知的全部,程序可行,因?yàn)槲铱梢蕴砑?分割或我選擇的其他任何東西,但在那之后,我希望它給我選擇一個(gè)不同的操作,我在返回后把“choice = null”,但它似乎沒(méi)有工作,我試過(guò)到目前為止有幾件事,但我開(kāi)始認(rèn)為我可能誤解了實(shí)際上有什么回報(bào),所以我認(rèn)為最好轉(zhuǎn)向你們尋求幫助.

任何建議表示贊賞,謝謝!

解決方法:

代替

return adition(result, result);

在你的adition方法中,返回結(jié)果.

否則你只是重復(fù)相同的方法,直到堆棧溢出. (同樣適用于其他方法).

你的while(選擇!= null){loop是沒(méi)有必要的,因?yàn)槟憧偸窃O(shè)置choice = null;.既然您也沒(méi)有在該循環(huán)中更改選擇的值,您也可以刪除循環(huán).

將參數(shù)傳遞給adition(etc)方法沒(méi)有意義,因?yàn)槟偸菚?huì)覆蓋它們.只需在這些方法中將它們聲明為局部變量:

public static float adition() {  // No parameters
    // ...
    float n1 = input.nextFloat();
    // ... 
    float n2 = input.nextFloat();
來(lái)源:https://www./content-1-351651.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多