You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何解决do-while循环字符串比较报错并实现多操作重复执行?

问题

我编写了一段Java代码,想要实现重复执行add、subtract、multiply三种运算,输入非法操作时终止程序,但运行时提示operation cannot be resolved错误。

我的代码如下:

import java.util.Scanner;
public class WorkArea {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        
        String errorMessage="Invalid input entered. Terminating...";
        
        do {
        System.out.println("Operations: add subtract multiply");
        System.out.println("Enter an operation:");
        String operation=input.next();

        switch(operation.toLowerCase())
        {
        case "add":
            System.out.println("Enter two integers:");
            if(input.hasNextInt())
            {
                int int1=input.nextInt();
                    if(input.hasNextInt())
                    {
                        int int2=input.nextInt();
                        System.out.println("Answer:"+(int1+int2));
                    }else{
                        System.out.println(errorMessage);
                    }
            }else{
                System.out.println(errorMessage);
            }
            break;
        case "subtract":
            System.out.println("Enter two integers:");
            if(input.hasNextInt())
            {
                int int1=input.nextInt();
                    if(input.hasNextInt())
                    {
                        int int2=input.nextInt();
                        System.out.println("Answer:"+(int1-int2));
                    }else{
                        System.out.println(errorMessage);
                    }
            }else{
                    System.out.println(errorMessage);
                }
            break;
        case "multiply":
            System.out.println("Enter two integers:");
            if(input.hasNextDouble())
            {
                double double1=input.nextDouble();
                    if(input.hasNextDouble())
                    {
                        double double2=input.nextDouble();
                        System.out.printf("Answer:%.2f\n",double1*double2);
                    }else{
                        System.out.println(errorMessage);
                    }
            }else{
                    System.out.println(errorMessage);
                }
            break;
        
        default:
            System.out.println(errorMessage);
            break;
        }
    }while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
}
}

我尝试过几种while条件写法,但都没解决问题:

while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
while(!operation=="add"||"subtract"||"multiply");
while(!operation.equals("multiply"));

请问如何正确实现该功能,在单个控制台窗口中执行多次合法运算?


解决方案

1. 修复变量作用域问题

报错operation cannot be resolved的核心原因是:operation变量在do代码块内部声明,而while条件判断在代码块外部,超出了变量的作用域范围。需将operation的声明移到do-while循环外部。

2. 修正循环逻辑

原while条件逻辑错误:!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply") 永远为true(一个字符串不可能同时等于三个值,总有一个!equals结果为真),会导致无限循环。正确逻辑应为:输入合法操作(add/subtract/multiply)时继续循环,输入非法操作时终止。

3. 额外优化点

  • 字符串比较禁止用==,必须用equals()或equalsIgnoreCase(),因为==比较的是对象引用而非内容。
  • 输入非法数值时,需清空Scanner缓冲区,避免后续输入被错误读取。

修正后的完整代码

import java.util.Scanner;

public class WorkArea {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String errorMessage = "Invalid input entered. Terminating...";
        // 将operation声明移到循环外部,扩大作用域
        String operation;

        do {
            System.out.println("Operations: add subtract multiply");
            System.out.println("Enter an operation:");
            operation = input.next();

            switch (operation.toLowerCase()) {
                case "add":
                    System.out.println("Enter two integers:");
                    if (input.hasNextInt()) {
                        int int1 = input.nextInt();
                        if (input.hasNextInt()) {
                            int int2 = input.nextInt();
                            System.out.println("Answer:" + (int1 + int2));
                        } else {
                            System.out.println(errorMessage);
                            // 清空非法输入,避免干扰下一次循环
                            input.next();
                            // 输入非法数值时终止程序
                            return;
                        }
                    } else {
                        System.out.println(errorMessage);
                        input.next();
                        return;
                    }
                    break;
                case "subtract":
                    System.out.println("Enter two integers:");
                    if (input.hasNextInt()) {
                        int int1 = input.nextInt();
                        if (input.hasNextInt()) {
                            int int2 = input.nextInt();
                            System.out.println("Answer:" + (int1 - int2));
                        } else {
                            System.out.println(errorMessage);
                            input.next();
                            return;
                        }
                    } else {
                        System.out.println(errorMessage);
                        input.next();
                        return;
                    }
                    break;
                case "multiply":
                    System.out.println("Enter two numbers:");
                    if (input.hasNextDouble()) {
                        double double1 = input.nextDouble();
                        if (input.hasNextDouble()) {
                            double double2 = input.nextDouble();
                            System.out.printf("Answer:%.2f\n", double1 * double2);
                        } else {
                            System.out.println(errorMessage);
                            input.next();
                            return;
                        }
                    } else {
                        System.out.println(errorMessage);
                        input.next();
                        return;
                    }
                    break;
                default:
                    System.out.println(errorMessage);
                    // 输入非法操作时终止程序
                    return;
            }
        // 循环条件:操作是合法值时继续循环
        } while (operation.equalsIgnoreCase("add") || operation.equalsIgnoreCase("subtract") || operation.equalsIgnoreCase("multiply"));
    }
}

代码说明

  • 把operation变量声明移到do-while循环外,确保while条件能访问该变量。
  • 循环条件改为判断当前操作是否为合法值,合法则继续循环,非法则终止。
  • 输入非法数值时,用input.next()清空缓冲区,避免残留输入干扰后续循环,随后直接return终止程序。
  • 用equalsIgnoreCase()简化大小写不敏感的判断逻辑。

内容的提问来源于stack exchange,提问作者Priyadhanam Priyadhanam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:10:13