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

Java代码在IDE运行无输出,是否属于运行时问题求助

代码无输出原因
  • 代码中使用的Scanner.nextInt()为阻塞式方法,执行到该方法时程序会暂停等待控制台输入整数,未完成全部输入前不会执行到后续打印逻辑,因此看不到任何输出。
  • 原代码需要完整输入 n + 2 个整数才会触发输出:第一个整数为人数n,第二个为首个人员体重,后续for循环还要求输入n个整数,输入数量不足时程序会一直处于等待状态。
  • 原代码存在输入逻辑冗余问题:统计n个人的体重仅需要输入1次人数+ n次体重即可,现有逻辑多要求了一次输入,可调整为如下写法,减少不必要的输入要求:
package Fall21;

import java.util.Scanner;

public class BalloonRideWeight {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int totalWeight = 0;
        
        for (int i = 0; i < n; ++i) {
            int personWeight = sc.nextInt();
            totalWeight += personWeight;
        }
        
        if (totalWeight > 500) {
            System.out.println("Everyone too fat.");
        } else {
            System.out.println("Just right.");
        }
        sc.close();
    }
}
是否属于运行时问题
  • 如果是指运行时异常,当前无输出的情况不属于:运行时异常的表现是程序报错终止,当前程序属于正常等待输入的运行状态,没有触发错误。
  • 仅当输入内容不符合要求(比如输入非整数内容)、输入流提前关闭时,才会抛出InputMismatchException、NoSuchElementException这类运行时异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 06:54:03