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

Eclipse中Java代码编译正常但运行挂起后终止问题求助

问题根因

代码编译通过但运行挂起终止,本质是代码结构错误触发无限死循环,和Eclipse IDE无关,重启IDE、重建项目都无法解决逻辑层面的问题。
具体错误点:

  • 主方法写法错误:你写业务逻辑的Project1类中,main方法是完全空的,所有创建重量对象、计算最值/平均值、打印结果的代码,都被错误写到了main方法外部的实例初始化块中,默认不会执行;最外层Weight_Class的main方法仅定义了两个局部类,没有任何执行业务的代码,程序入口逻辑完全混乱。
  • 类嵌套结构非法:你把Weight、Project1两个核心业务类全部作为局部类,嵌套在Weight_Class的main方法内部,这种写法很容易触发类加载、成员变量初始化顺序异常。如果换算常量ouncesInPounds没有被正确赋值为16,normalizeOunces方法里的while循环就会进入无限死循环,占满CPU资源后被系统强制终止,表现就是程序挂起无响应后直接退出。
  • 存在冗余错误代码:Project1类中无意义的public static final String java = null;声明、注释掉的非法类定义语句,都会干扰类的正常加载。
  • 逻辑实现不严谨:重量换算值ouncesInPounds应该定义为类级别的静态常量,而不是每个实例都持有的普通成员变量,避免出现意外赋值;normalizeOunces方法仅处理了盎司大于16的进位场景,没有处理负数盎司的借位逻辑,鲁棒性不足。
修复方案

调整类结构,修正main方法位置,移除无效代码,修正常量定义:

  1. 把Weight类调整为独立业务类,将ouncesInPounds定义为private static final double类型的静态常量
  2. 把main方法直接放到最外层公共类中,将业务代码全部移入main方法内部,删除空main、无效静态变量、错误注释
  3. 补全normalizeOunces的边界处理逻辑,增加除数为0的校验
修复后可正常运行的代码
package assignment_One;

class Weight {
    // 盎司转磅的静态常量,全局唯一
    private static final double OUNCES_PER_POUND = 16;
    private int pounds;
    private double ounces;

    public Weight(int pounds, double ounces) {
        this.pounds = pounds;
        this.ounces = ounces;
        normalizeOunces();
    }

    public boolean lessThan(Weight wt) {
        if (this.pounds < wt.pounds) {
            return true;
        } else if (this.pounds > wt.pounds) {
            return false;
        } else {
            return this.ounces < wt.ounces;
        }
    }

    public void addTo(Weight wt) {
        this.pounds += wt.pounds;
        this.ounces += wt.ounces;
        normalizeOunces();
    }

    @Override
    public String toString() {
        return pounds + " lbs " + String.format("%.2f", ounces) + " oz";
    }

    private double toOunces() {
        return pounds * OUNCES_PER_POUND + ounces;
    }

    private void normalizeOunces() {
        // 处理盎司大于等于16的进位
        while (ounces >= OUNCES_PER_POUND) {
            ounces -= OUNCES_PER_POUND;
            pounds++;
        }
        // 处理盎司为负的借位
        while (ounces < 0) {
            ounces += OUNCES_PER_POUND;
            pounds--;
        }
    }

    public void getAvg(int n) {
        if (n == 0) {
            throw new ArithmeticException("除数不能为0");
        }
        ounces = toOunces();
        pounds = 0;
        ounces = ounces / n;
        normalizeOunces();
    }
}

public class Weight_Class {
    private static Weight findMinimum(Weight w1, Weight w2, Weight w3) {
        if (w1.lessThan(w2) && w1.lessThan(w3)) {
            return w1;
        } else if (w2.lessThan(w1) && w2.lessThan(w3)) {
            return w2;
        } else {
            return w3;
        }
    }

    private static Weight findMaximum(Weight w1, Weight w2, Weight w3) {
        if (!w1.lessThan(w2) && !w1.lessThan(w3)) {
            return w1;
        } else if (!w2.lessThan(w1) && !w2.lessThan(w3)) {
            return w2;
        } else {
            return w3;
        }
    }

    private static Weight findAverage(Weight w1, Weight w2, Weight w3) {
        Weight wt = new Weight(0, 0);
        wt.addTo(w1);
        wt.addTo(w2);
        wt.addTo(w3);
        wt.getAvg(3);
        return wt;
    }

    public static void main(String[] args) {
        Weight weight1 = new Weight(25, 20);
        Weight weight2 = new Weight(30, 8);
        Weight weight3 = new Weight(23, 10);

        System.out.println("Weight 1: " + weight1);
        System.out.println("Weight 2: " + weight2);
        System.out.println("Weight 3: " + weight3);

        Weight min = findMinimum(weight1, weight2, weight3);
        System.out.println("\nMinimum weight: " + min);

        Weight max = findMaximum(weight1, weight2, weight3);
        System.out.println("\nMaximum weight: " + max);

        Weight avg = findAverage(weight1, weight2, weight3);
        System.out.println("\nAverage weight: " + avg);
    }
}
正常运行输出
Weight 1: 26 lbs 4.00 oz
Weight 2: 30 lbs 8.00 oz
Weight 3: 23 lbs 10.00 oz

Minimum weight: 23 lbs 10.00 oz

Maximum weight: 30 lbs 8.00 oz

Average weight: 26 lbs 12.67 oz

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 20:42:16