[英]Program terminates in eclipse
我正在处理一个类项目并且整天都在处理这个代码,逻辑似乎在 IDE 中编译,但是当我运行它时,它只是挂起然后终止。 我已经尝试关闭和打开 IDE,甚至创建了新项目,但我似乎无法解决这个问题。 我正在使用日食。 这是代码。
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());
}
}
}
}
要显示根本问题,请查看代码结构 - 删除大部分语句和注释以方便可视化:
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
方法中都没有声明。 // create three weight objects
之后的语句不在任何main
方法中,而是在 Instance Initializer 块中!
很可能您想要类似于以下结构的东西
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 1:将static
添加到嵌套(内部)类,因此它们不需要包含类。
Obs 2:发布的代码旨在显示整体结构并且不完整 - 它们不会编译!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.