簡體   English   中英

給出答案后,如何使計算器重新啟動

[英]How do I make my calculator restart after giving the answers

因此,我嘗試在控制台上制作Java計算器,並且效果不錯,但是當我得到結果時,我希望它像循環一樣重新啟動,但我無法弄清楚。

import java.util.Scanner;

public class calculator {

    public static void main(String[] args0) {
        Scanner test = new Scanner(System.in);
        int x;
        int y;
        String c;
        try {
            System.out.println("Insert a number ");
            x = test.nextInt();
            System.out.println("insert a value e.g * / + -");
            c = test.next();
            System.out.println("Insert another number");
            y = test.nextInt();

            if (c.equals("*")) {
                System.out.println("the total is " + x * y);
            } else if (c.equals("+")) {
                System.out.println("the total is " + (x + y));
            } else if (c.equals("-")) {
                System.out.println("the total is " + (x - y));
            } else if (c.equals("/")) {
                System.out.println("the total is " + (x / y));
            } else {
                System.out.println("you are an idiot");
            }
        } catch (Exception e) {
            System.out.println("Please enter correct value.");
        }
    }
}

使用while + break 以下是工作版本:

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args0) {
        Scanner test = new Scanner(System.in);

        int x;
        int y;
        String c;

        while (true) {
            try {
                System.out.println("Insert a number ");
                x = test.nextInt();

                System.out.println("insert a value e.g * / + -");
                c = test.next();

                System.out.println("Insert another number");
                y = test.nextInt();

                if (c.equals("*")) {
                    System.out.println("the total is " + x * y);
                } else if (c.equals("+")) {
                    System.out.println("the total is " + (x + y));

                } else if (c.equals("-")) {
                    System.out.println("the total is " + (x - y));
                } else if (c.equals("/")) {
                    System.out.println("the total is " + (x / y));
                } else {
                    System.out.println("you are an idiot");
                }

            } catch (Exception e) {
                System.err.println("Please enter correct value.");
            }

            System.out.println("Do you wish to continue(y/n)?");
            if (test.next().equals("n")) {
                break;
            }
        }

    }
}

您可以使用while循環:

public static void main(String[] args0) {
    Scanner test = new Scanner(System.in);
    while(true) {
    ....

}

要停止計算器時,需要輸入ctrl - c

另一種方法,使用布爾值flag ,讓用戶決定是否繼續:

public static void main(String[] args0) {
    boolean flag = true;
    Scanner test = new Scanner(System.in);
    while(flag) {
        ....
        System.out.println("enter y for continue, n for exit");
        c = test.next();
        if (c.equals("n")) {
           flag = false;
        } 
    }
}

在try之前插入while循環,然后將所有內容放到它下面。放while(true),這將無限重復。 如果您想按某個鍵停止條件,並在其中插入中斷。

Scanner sc=new Scanner(System.in);
while(true){
...
if(sc.next()=='q') break; //this is if you want to quit after pressing q
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM