简体   繁体   English

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

[英]Execution of do while loop in the Java Calculator program

In my Java Program, I have used a Boolean variable 'decision' which should execute the actual calculator code in the 'do loop code block' only when the variable is true, but the do while loop is executed anyways even when the decision variable is false.在我的 Java 程序中,我使用了一个布尔变量“decision”,它应该只在变量为真时才执行“do 循环代码块”中的实际计算器代码,但是即使当决策变量为真时,do while 循环还是会执行错误的。 I am using Eclipse IDE for Java and JDK 10 (both are recent versions).我使用 Eclipse IDE for Java 和 JDK 10(都是最新版本)。 Please help me with a solution.请帮我解决。 The code is as below代码如下

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

the do while loop is executed anyways even when the decision variable is false即使决策变量为假,do while 循环也会执行

A do...while will executes the body once before checking the condition. do...while将在检查条件之前执行一次主体。

Either modify your code for while or add an if-else enclosing do...while .要么修改你的代码while或添加if-else封闭do...while

You can add another case 5 and in that write System.exit(0) for the successful termination of the program.您可以添加另一个case 5并在其中写入System.exit(0)以成功终止程序。

In the while part pass (option != 5) .while部分 pass (option != 5) This should run the program.这应该运行程序。 You don't need decision variable.你不需要决策变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM