当前位置: 首页 > 知识库问答 >
问题:

程序在日食中终止

凌博实
2023-03-14

我正在处理一个类项目,并且一整天都在处理这段代码,逻辑似乎在IDE中编译,但当我运行它时,它只是挂起然后终止。我尝试过关闭和打开IDE,甚至创建了新项目,但我似乎无法解决这个问题。我正在使用eclipse。这是代码

package assignment_One;
public class Weight_Class {
    public static void main(String[] args) {
        // a class that represents a Weight in pounds and ounces
        class Weight {
           // private instance variables
           private int pounds;
           private double ounces;
           // a private named constant that defines the number of ounces in a pound
           private double ouncesInPounds = 16;
           
           // constructor that allows the pounds and ounces to be initialized 
           // to the values supplied as parameters
           public Weight(int pounds, double ounces) {
               this.pounds = pounds;
               this.ounces = ounces;
               // normalize the ounces
               normalizeOunces();
           
        } 
           // a method that checks if the current object's weight is less than 
           // the object passed as a parameter to the method
           public boolean lessThan(Weight wt) {
               // return true if the pounds of the current object is less
               if(this.pounds < wt.pounds) 
                   return true;
               // return false if the pounds of the current object is more
               else if(this.pounds > wt.pounds)
                   return false;
               // else both the weights have the same pounds
               else {
                   // return true if the ounces of the current object is less
                   if(this.ounces < wt.ounces)
                       return true;
                   // return false if the ounces of the current object is more
                   else
                       return false;
               }
           }
           // a method that adds weight to the current object
           public void addTo(Weight wt) {
               // add the pounds and ounces of the called object to the current object
               this.pounds += wt.pounds;
               this.ounces += wt.ounces;
               // normalize the ounces
               normalizeOunces();
           }
           // a method that returns the string representation of the object
           public String toString() {
               return pounds + " lbs " + String.format("%.2f", ounces) + " oz";
           }
           // a method that returns the total number of ounces in the called object
           private double toOunces() {
               return pounds * ouncesInPounds + ounces;
           }
           // a method that normalizes the weight on which it was invoked
           private void normalizeOunces() {
               // loop until the no. of ounces is less than the no. of ounces in a pound
               while(ounces > ouncesInPounds) {
                   // decrement 16 ounces from the ounces and increment pound by 1
                   ounces -= ouncesInPounds;
                   pounds++;
               }
           }
           // I'm adding an additional method named getAvg()
           // this method will accept a number and divide the current object's weight
           // by the given number & then normalize the weight
           public void getAvg(int n) {
               // convert the current object's weight to total ounces
               ounces = toOunces();
               // reinitialize the current object's pound to 0
               pounds = 0;
               // divide the total ounces by the given number
               ounces = ounces / n;
               // normalize the ounces
               normalizeOunces();
           }
        }
        //public class Project1.java;
        class Project1 {
           // a method that returns the smallest weight among 3 weights
           public static final String java = null;
        private static Weight findMinimum(Weight w1, Weight w2, Weight w3) {
               // if the first weight is smaller than the second & third weight
               if(w1.lessThan(w2) && w1.lessThan(w3))
                   // return the first weight as the smallest weight
                   return w1;
               // else if the second weight is smaller than the first & third weight
               else if(w2.lessThan(w1) && w2.lessThan(w3))
                   // return the second weight as the smallest weight
                   return w2;
               // else the third weight is smaller than the first & second weight
               else
                   // return the third weight as the smallest weight
                   return w3;
           }
           // a method that returns the highest weight among 3 weights
           private static Weight findMaximum(Weight w1, Weight w2, Weight w3) {
               // if the first weight is greater than the second & third weight
               if(!w1.lessThan(w2) && !w1.lessThan(w3))
                   // return the first weight as the highest weight
                   return w1;
               // else if the second weight is greater than the first & third weight
               else if(!w2.lessThan(w1) && !w2.lessThan(w3))
                   // return the second weight as the highest weight
                   return w2;
               // else the third weight is greater than the first & second weight
               else
                   // return the third weight as the highest weight
                   return w3;
           }
           // a method that returns the average weight among 3 weights
           private static Weight findAverage(Weight w1, Weight w2, Weight w3) {
               // create a new Weight object
               Weight wt = new Weight(0, 0);
               // add the weight of the three objects to the new Weight object
               // by invoking the addTo() method
               wt.addTo(w1);
               wt.addTo(w2);
               wt.addTo(w3);
               // invoke the getAvg() method to get the average weight
               wt.getAvg(3);
               // return the average weight
               return wt;
           }
           // main method
           @SuppressWarnings("unused")
        public static void main(final String[] args) {
        } {
               // create three weight objects
               Weight weight1 = new Weight(25, 20);
               Weight weight2 = new Weight(30, 8);
               Weight weight3 = new Weight(23, 10);
               // display the 3 weights
               System.out.println("Weight 1: "+weight1.toString());
               System.out.println("Weight 2: "+weight2.toString());
               System.out.println("Weight 3: "+weight3.toString());
               // invoke the findMinimum() method to get the smallest weight among the 3 weights
               Weight min = findMinimum(weight1, weight2, weight3);
               // display the minimum of the 3 weights
               System.out.println("\nMinimum weight: "+min.toString());
               
               // invoke the findMaximum() method to get the highest weight among the 3 weights
               Weight max = findMaximum(weight1, weight2, weight3);
               // display the maximum of the 3 weights
               System.out.println("\nMaximum weight: "+max.toString());
               
               // invoke the findAverage() method to get the average weight of the 3 weights
               Weight avg = findAverage(weight1, weight2, weight3);
               // display the average of the 3 weights
               System.out.println("\nAverage weight: "+avg.toString());
           }
        }   
    }
}

共有1个答案

蒲寂离
2023-03-14

要显示根本问题,请参阅代码的结构-删除了大多数语句和注释:

java prettyprint-override">public class Weight_Class {
    
    public static void main(String[] args) {
        
        class Weight {
            // MEMBERS DELETED
        }

        class Project1 {
            // MEMBERS DELETED

            @SuppressWarnings("unused")
            public static void main(final String[] args) {
                // NO STATEMENTS
            }
            
            {
                // create three weight objects
                Weight weight1 = new Weight(25, 20);
                Weight weight2 = new Weight(30, 8);
                Weight weight3 = new Weight(23, 10);
                ...                
                System.out.println("\nAverage weight: "+avg.toString());
            }
        } 

        // NO STATEMENTS  
    }
}

我们现在可以看到main方法中都没有语句。//创建三个权重对象之后的语句不在任何main方法中,而是在实例初始化器块中!

很可能您想要类似于以下结构的东西

public class Weight_Class {
    
    public static void main(String[] args) {
        // create three weight objects
        Weight weight1 = new Weight(25, 20);
        Weight weight2 = new Weight(30, 8);
        Weight weight3 = new Weight(23, 10);
        ...                
        System.out.println("\nAverage weight: "+avg.toString());

    }
    
    static class Weight {
        // MEMBERS DELETED
    }
    
    static class Project1 {
        // MEMBERS DELETED
        
        // NO main METHOD HERE
    } 
}

Obs:发布的代码旨在显示整体结构,并不完整-它们不会编译!

 类似资料:
  • 我正在尝试为 eclipse 中的 gradle 多 Web 应用程序应用程序设置一个开发环境。该应用程序部署在码头上的生产环境中,因此这就是我想要在开发中使用的应用程序。我在让eclipse运行所有Web应用程序并能够调试时遇到问题。 我能够找到的解决方案在线使用只能运行单个webapps的插件。或者他们通过服务器中的gradle(gretty)运行webapps,然后导致调试问题。 我的来源是

  • 问题内容: 我已经研究了一段时间,但我不得不问:现在通过Eclipse是否可以完成?我已经找到有关Maven和Ant的答案,但我从未使用过。如果我的输出.jar文件是可运行的jar,则可以通过eclipse完成,但这是一个使用外部jar的库。 那么,是否有我想念的东西,或者这是我的Maven / Ant路径? 编辑:关于Jarrod的答案,我的时间表很紧,我不能花很多时间完成这项工作(感谢提示,“

  • 几周前,我切换到Eclipse Oxygen,并开始出现如下错误。 project facet JST.AppClient的8.0版本不存在。 eclipse.buildid=4.7.1.m20171009-0410 java.version=1.8.0_144 java.vendor=Oracle Corporation BootLoader常量:OS=win32,arch=x86_64,ws=

  • 我在eclipse中将scala项目转换为使用Maven(只需右键单击project并配置Maven build),这就创建了pom。xml,添加了正确的依赖项,它从maven存储库中提取了所需的JAR,但每当我尝试编译时,我都看不到在target\classes文件夹中生成类文件。然而,我在target\classes文件夹下的相应目录中看到了scala文件的实际源代码。我不确定它为什么要复制t

  • 由于“Android库更新”从未完成,我在Eclipse中处于停滞状态。我尝试重新启动 Eclipse,重新启动计算机,删除 Eclipse 工作区下的 .metadata 文件夹,以及在黑暗中拍摄其他几个镜头。在窗口下 - 我只打开了两个项目:一个小型演示应用程序和与之链接的Google Play服务库。所以问题不可能是项目太多,每个这个问题。 有人有其他想法吗?

  • 在linux中,当主线程终止时,整个过程都会退出,不管它是以何种方式终止的,都是通过函数out()或从main返回。如果主线程从main()返回,它将返回到称为crt. o或类似的“C运行时”。在crt. o中,它的c代码是这样的:退出(main(argc, argv));退出()将由主线程 最终调用,结果,所有线程都终止。 我的想法正确吗? 如果在crt中。o exit()被诸如void thr