Java计算器程序中do-while循环的执行

Java计算器程序中do-while循环的执行,java,calculator,do-while,Java,Calculator,Do While,在我的Java程序中,我使用了一个布尔变量'decision',它应该只在变量为true时执行'do loop code block'中的实际计算器代码,但即使在决策变量为false时,do while循环也会执行。我正在使用EclipseIDEforJava和JDK10(都是最新版本)。请帮我找个解决办法。代码如下 import java.util.Scanner; public class apples { public static void main(String[] args) {

在我的Java程序中,我使用了一个布尔变量'decision',它应该只在变量为true时执行'do loop code block'中的实际计算器代码,但即使在决策变量为false时,do while循环也会执行。我正在使用EclipseIDEforJava和JDK10(都是最新版本)。请帮我找个解决办法。代码如下

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);
  }
}
即使决策变量 是假的

do…while
将在检查条件之前执行主体一次


修改
的while
代码或添加
if-else
包含
do…while
您可以添加另一个
案例5
并在该写入
系统中。退出(0)
以成功终止程序

中,同时
零件传递
(选项!=5)
。这将运行程序。
您不需要决策变量

您可以使用if/else来确定是否要执行循环。或者您只需更改为使用纯while循环。这就是
所做的。..while
循环的用途。正文将至少执行一次。使用一段时间,谢谢大家的建议和修复。帮助了我。问题解决了!谢谢大家的建议和修正。帮助了我。问题解决了!