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

如何通过用户输入字符串终止do-while循环?循环后代码未执行

问题分析与解决方案

你的循环无法终止的核心原因是do-while的终止条件逻辑错误:你把字符串数组dognames和字符串"quit"做相等比较,而数组和字符串是完全不同的对象,"quit".equals(dognames)永远返回false,导致!"quit".equals(dognames)永远为true,循环无限执行。

另外你的循环结构也存在问题:每次进入do块都会强制输入dogs条狗的信息,用户没有中途终止的机会,且输入完一轮后会重新开始输入,永远到不了后续的计算代码。

修正方案一:输入完一轮后询问是否继续循环

如果你的需求是先输入指定数量的狗的信息,再选择是否输入新一轮信息,可按以下方式修改:

import java.util.Scanner;

public class DogWeightCalculator {
    public static void main(String... args) {
        Scanner input = new Scanner(System.in);
        int dogs;

        System.out.println("How many dogs?");
        dogs = input.nextInt();

        String[] dognames = new String[dogs];
        Double[] dogweights = new Double[dogs];
        String continueInput;

        do {
            // 输入一轮狗的信息
            for (int i = 0; i < dogs; i++) {
                System.out.println("Enter dog name and weight: ");
                dognames[i] = input.next();
                dogweights[i] = input.nextDouble();
            }

            // 询问是否继续输入
            System.out.println("Enter 'quit' to stop, or any other key to continue:");
            continueInput = input.next();
        } while (!"quit".equals(continueInput)); // 用输入的字符串判断是否终止循环

        // 计算平均体重
        double sum = 0;
        for (double weight : dogweights) {
            sum += weight;
        }
        double dogweightsaverage = sum / dogweights.length;

        System.out.println("Average dog weight: " + dogweightsaverage);
        System.out.print("test");
    }
}

修正方案二:允许中途输入"quit"终止循环

如果想要在输入任意一条狗的信息时,输入"quit"直接终止整个流程,可修改为:

import java.util.Scanner;

public class DogWeightCalculator {
    public static void main(String... args) {
        Scanner input = new Scanner(System.in);
        int dogs;

        System.out.println("How many dogs?");
        dogs = input.nextInt();

        String[] dognames = new String[dogs];
        Double[] dogweights = new Double[dogs];
        boolean quit = false;

        for (int i = 0; i < dogs && !quit; i++) {
            System.out.println("Enter dog name and weight (or 'quit' to stop): ");
            String name = input.next();
            if ("quit".equals(name)) {
                quit = true;
                break;
            }
            dognames[i] = name;
            dogweights[i] = input.nextDouble();
        }

        // 未中途退出时才计算平均体重
        if (!quit) {
            double sum = 0;
            for (double weight : dogweights) {
                sum += weight;
            }
            double dogweightsaverage = sum / dogweights.length;

            System.out.println("Average dog weight: " + dogweightsaverage);
        }

        System.out.print("test");
    }
}

关键修正点总结

  • 不要直接把数组和字符串做equals比较,需针对数组元素或单独的输入字符串判断终止条件
  • 根据实际需求设计循环逻辑:要么一轮输入后询问是否继续,要么允许中途退出
  • 循环终止后,后续的计算代码才能正常执行

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 09:14:59