Javaの電卓プログラムでdo-while文の実行

ヨギラジ:

私のJavaのプログラムでは、私は、変数が真である場合にのみ、「doループのコード・ブロック」で実際の計算コードを実行すべきであるブール変数の決定」を使用していたが、決定変数がある場合でも、ループはとにかく実行されながらやります偽。私は、JavaとJDK 10(両方とも最近のバージョンです)のためのEclipse IDEを使用しています。ソリューションで私を助けてください。コードは以下の通りであります

import java.util.Scanner;

public class apples {

public static void main(String[] args) {

    int option,a,b,c;
    boolean decision;
    System.out.println("We are here to create a calculator");
    System.out.print("Do you want to switch on the calculator:");
    Scanner yogi = new Scanner(System.in);
    decision = yogi.nextBoolean();
    do {
        System.out.println("Following operations are available to perform:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Enter the operations do you want to perform:");
        option = yogi.nextInt();
        System.out.println("Choice of operation is:"+option);

        switch(option) {

        case 1:
            System.out.println("Enter two numbers to be added:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a + b;
            System.out.print("Addition:"+c);
            break;

        case 2:
            System.out.println("Enter two numbers to be subtracted:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a - b;
            System.out.print("subtracted:"+c);
            break;

        case 3:
            System.out.println("Enter two numbers to be multiplied:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a * b;
            System.out.print("multiplied:"+c);
            break;

        case 4:
            System.out.println("Enter two numbers to be divided:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a / b;
            System.out.print("divided:"+c);
            break;

        default:
            System.out.println("This is a wrong choice");
            break;
        }
    }while(decision==true);
  }
}
ゴクール・ナスKP:

決定変数が偽の場合でも、do-while文はとにかく実行されます

do...while意志が一度状態をチェックする前に、本体を実行します。

どちらかのためにあなたのコードを変更whileまたは追加if-else囲んをdo...while

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=192067&siteId=1