为了账号安全,请及时绑定邮箱和手机立即绑定

在Java Calculator程序中执行do while循环

在Java Calculator程序中执行do while循环

慕慕森 2021-05-03 17:53:40
在我的Java程序中,我使用了布尔变量“ decision”,该变量仅在变量为true时才应在“ 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);  }}
查看完整描述

2 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

您可以添加另一个,case 5并在该写入System.exit(0)中成功终止程序。

while部分通过(option != 5)。这应该运行该程序。您不需要决策变量。


查看完整回答
反对 回复 2021-05-12
  • 2 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信