0

電卓を作ろうとしています。これらの演算子: x+-、正常に/動作します。

しかし、ユーザーが数学の問題の答えを得た後、2 つのことを実行できるようにしたいと考えています。

続行するかどうかをユーザーに尋ねます。

  1. ユーザーyesが入力すると、再びカウントされる2つの数字を入力できます。
  2. ユーザーがno単にシャットダウンした場合。

これが私のコードです:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner Minscanner = new Scanner(System.in);
        int nr1 = Integer.parseInt(Minscanner.nextLine());
        int nr2 = Integer.parseInt(Minscanner.nextLine());
        int yes = Integer.parseInt(Minscanner.nextLine());//trying to fix reset
        int ans =0;
        int reset = J;/trying to make it reset if user types in yes
        String anvin = Minscanner.nextLine();

        if(anvin.equalsIgnoreCase("+")) {
            ans = nr1 + nr2;
        }
        else if(anvin.equalsIgnoreCase("-")) {
            ans = nr1 - nr2;
        }
        else if(anvin.equalsIgnoreCase("*")) {
            ans = nr1 * nr2;
        }
        else if(anvin.equalsIgnoreCase("/")) {
            ans = nr1 / nr2;
            System.out.println(ans);
        }
        if(anvin.equalsIgnoreCase("yes")) {
            return;
        }
    }
}
4

2 に答える 2

1

コードを

do {
    ...
} while (condition);

ループし、あなたの場合、条件はwantToContinueユーザーが「はい」と言った場合のようなものになります。

ユーザーが計算する必要がなくなるまで、プログラムは終了しません。

于 2013-09-10T11:52:45.130 に答える