【问题标题】:Problem in while loop in a simple java based calculator [duplicate]一个简单的基于 Java 的计算器中的 while 循环问题 [重复]
【发布时间】:2022-12-04 01:46:50
【问题描述】:

我试图制作一个简单的基于 Java 的计算器,用户可以在其中输入两位数字,选择计算类型并获得答案。主要特点是在第一次计算后,用户可以决定是用新数字重复计算还是退出计算器。为此,我将整个代码放在一个 while 循环中。在 while 循环结束时,我选择了使用扫描器对象更新循环变量的选项。这样,如果用户按“Y”键,计算器将重新运行,并且在按任何其他键时,该过程将完成。

计算器运行良好,但重新运行计算器或退出进程的选项不起作用。在更新循环变量时,无论用户输入如何,过程都会完成并且循环不会重复。请告诉我我在这里做错了什么

输出样本

import java.util.Scanner;

public class Calculator
{
    public static void main(String[] args)
    {



        Scanner sc=new Scanner(System.in);

        double x,y;
        String flag="Y";  //loop variable to run the loop
        while(flag=="Y") //loop to make calculator run as many times user wants
        {
            System.out.println("Enter numbers to be calculated");

            x = sc.nextDouble();
            y = sc.nextDouble();
            System.out.println("1. Add");
            System.out.println("2. Subtract");
            System.out.println("3. Multiply");
            System.out.println("4. Divide");
            System.out.println("Enter Your choice");
            int s=sc.nextInt();
            switch (s) {
                case 1:
                    System.out.print("Sum is : ");
                    System.out.println(x+y);
                    break;
                case 2:
                    System.out.print("Difference is : ");
                    System.out.println(x-y);
                    break;
                case 3:

                    System.out.println("The Product is");
                        System.out.println();
                        break;
                case 4:
                    try {
                        if(y==0)
                        { throw new ArithmeticException();}

                        else {
                            System.out.println("Division is : ");
                            System.out.println(x/y);
                        }
                    }
                    catch (ArithmeticException e)
                    {
                        System.out.println("Cant divide by zero");
                        System.out.println(e);
                        continue;
                    }
                    break;
                default:
                    System.out.println("Invalid choice");
               }
               sc.nextLine();
            System.out.println("Press Y to repeat and any other key to turn off calculator");
            flag=sc.nextLine(); //to take input from the user
            if(flag=="Y")
            {
                continue;  //if user enters Y the control should move back to starting of while loop
            }
            else
            {
                break; //if user presses any other key, the control should move out of loop and                      enter image description hereprogram should terminate
            }

            }

        }
    }

【问题讨论】:

  • 不要将字符串与 == 进行比较。请改用equals()

标签: java


【解决方案1】:

而不是使用 == 运算符来检查使用 equals() 方法或 equalsIgnoreCase() 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2011-07-23
    相关资源
    最近更新 更多