在我的Java程序中,我使用了布爾變量“ decision”,該變量僅在變量為true時才應(yīng)在“ do循環(huán)代碼塊”中執(zhí)行實際的計算器代碼,但是即使決策變量為錯誤的。我正在使用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貢獻(xiàn)1877條經(jīng)驗 獲得超1個贊
您可以添加另一個,case 5
并在該寫入System.exit(0)
中成功終止程序。
在while
部分通過(option != 5)
。這應(yīng)該運行該程序。您不需要決策變量。
添加回答
舉報
0/150
提交
取消