期望输出的数值未显示。

huangapple go评论78阅读模式
英文:

The values from expected output are not showing

问题

我实例化了SpaceStation类并调用了`addAstronaut`方法但是当我运行代码时输出中没有显示姓名重量高度和宇航员信息

SpaceStation.java:

public class SpaceStation {

   //私有成员
   private String name;
   private double stationWeight;
   private double altitude;

   private Astronaut[] Astronauts;
   private int totalAstronauts;

   //重载的构造函数
   public SpaceStation(String name, double 
   weight) {
      int altitude = 0;
      int totalAstronauts = 0; 
 }

  //方法
  public void addAstronaut(String name, double 
     height, double weight) {
     Astronauts = new Astronaut[3];
     stationWeight = stationWeight + weight;
     totalAstronauts++;  
  }

  public double setAltitude(double altitude) { return this.altitude = altitude; }

  public String toString() {
     return "SpaceStation: " + name + "\n" +
            "Weight(kg): " + stationWeight + "\n" +
            "Altitude(km): " + (altitude) + "\n" +
            "Astronauts: " + (totalAstronauts);
  }

  public static void main(String[] args) {
     SpaceStation aa = new SpaceStation("ISS", 419700.0);
     System.out.println(aa);
     aa.addAstronaut("Eli", 167.64, 81.65);
     aa.addAstronaut("John", 185.43, 100.30);
     aa.addAstronaut("Joey", 175.38, 90.38);
     aa.setAltitude(400.0);
  
  }

 }
英文:

I instantiated the SpaceStation class and called addAstronaut method but the name, weight, altitude, and astronauts are not showing in my output when I run it.

SpaceStation.java:

public class SpaceStation {
//private members
private String name;
private double stationWeight;
private double altitude;
private Astronaut[] Astronauts;
private int totalAstronauts;
//overloaded constructor
public SpaceStation(String name, double 
weight) {
int altitude = 0;
int totalAstronauts = 0; 
}
//method
public void addAstronaut(String name, double 
height, double weight) {
Astronauts = new Astronaut[3];
stationWeight = stationWeight + weight;
totalAstronauts++;  
}
public double setAltitude(double altitude) { return this.altitude = altitude; }
public String toString() {
return "SpaceStation: " + name + "\n" +
"Weight(kg): " + stationWeight + "\n" +
"Altitude(km): " + (altitude) + "\n" +
"Astronauts: " + (totalAstronauts);
}
public static void main(String[] args) {
SpaceStation aa = new SpaceStation("ISS", 419700.0);
System.out.println(aa);
aa.addAstronaut("Eli", 167.64, 81.65);
aa.addAstronaut("John", 185.43, 100.30);
aa.addAstronaut("Joey", 175.38, 90.38);
aa.setAltitude(400.0);
}
}

答案1

得分: 1

你的代码中存在四个错误:

1- 你没有在构造函数中设置实例变量 name 的内容。这导致输出时出现 SpaceStation: null。你需要在构造函数中设置名称。将构造函数更改为以下内容:

   public SpaceStation(String name, double 
   weight) {
	  this.name = name;    // 这一行被添加
      int altitude = 0;
      int totalAstronauts = 0; 
 }

2- 你在添加宇航员和设置高度之前就打印了 ss 的内容。在那个时候,还没有添加宇航员,所以重量、宇航员数量和高度都是0是正常的。如果在执行这些操作后再打印太空站的内容,就会正常工作:

  public static void main(String[] args) {
     SpaceStation ss = new SpaceStation("ISS", 419700.0);
     
     ss.addAstronaut("Smith", 167.64, 81.65);
     ss.addAstronaut("John", 185.43, 100.30);
     ss.addAstronaut("Joey", 175.38, 90.38);
     ss.setAltitude(400.0);
     
     System.out.println(ss);    // 这一行被移动到 addAstronaut 和 setAltitude 方法之后。
   
  }

3- 正如@sagi所指出的,构造函数中你声明并初始化了与类中另一个同名变量不同的变量。从技术上讲,双精度浮点数和整数已经初始化为0.0,所以你不会真正注意到,但是构造函数中的 altitudetotalAstronauts 变量是多余的。建议将构造函数更新为以下内容:

   public SpaceStation(String name, double 
   weight) {
	  this.name = name;
      altitude = 0;    // 移除了 "int"
      totalAstronauts = 0;  // 移除了 "int"
      Astronauts = new Astronaut[3];
 }

4- 正如@sagi所指出的,每次添加宇航员时都在重新初始化 Astronaut 数组。你需要在构造函数中初始化一次,然后每次添加宇航员时将宇航员放入数组中。

综合所有这些评论,SpaceStation 类应该如下所示:

package gov.nasa.spacevehicles;

import gov.nasa.personnel.Astronaut;

public class SpaceStation {

   // 私有成员
   private String name;
   private double stationWeight;
   private double altitude;

   private Astronaut[] Astronauts;
   private int totalAstronauts;

   // 重载构造函数
   public SpaceStation(String name, double 
   weight) {
	  this.name = name;
      altitude = 0;
      totalAstronauts = 0; 
      Astronauts = new Astronaut[3];
 }

  // 方法
  public void addAstronaut(String name, double 
     height, double weight) {
	 Astronauts[totalAstronauts] = new Astronaut(name, height, weight);
     stationWeight = stationWeight + weight;
     totalAstronauts++;  
  }

  public double setAltitude(double altitude) { return this.altitude = altitude; }

  public String toString() {
     return "SpaceStation: " + name + "\n" +
            "Weight(kg): " + stationWeight + "\n" +
            "Altitude(km): " + (altitude) + "\n" +
            "Astronauts: " + (totalAstronauts);
  }

  public static void main(String[] args) {
     SpaceStation ss = new SpaceStation("ISS", 419700.0);
     
     ss.addAstronaut("Smith", 167.64, 81.65);
     ss.addAstronaut("John", 185.43, 100.30);
     ss.addAstronaut("Joey", 175.38, 90.38);
     ss.setAltitude(400.0);
     
     System.out.println(ss);
   
  }

 }
英文:

Your code contains four errors:

1- You are not setting the content of the instance variable name in the constructor. This causes the line SpaceStation: null when printing the output. You need to set the name in the constructor. Change the constructor to be like this:

   public SpaceStation(String name, double 
weight) {
this.name = name;    // This line was added
int altitude = 0;
int totalAstronauts = 0; 
}

2- You are printing the content of ss before adding your astronauts and setting the altitude. At that time, there are no astronauts added, so it's normal that the weight, number of astronauts and altitude is 0. If you print the content of the space station after doing those operations, it's going to work:

  public static void main(String[] args) {
SpaceStation ss = new SpaceStation("ISS", 419700.0);
ss.addAstronaut("Smith", 167.64, 81.65);
ss.addAstronaut("John", 185.43, 100.30);
ss.addAstronaut("Joey", 175.38, 90.38);
ss.setAltitude(400.0);
System.out.println(ss);    // This line was moved after the addAstronaut and setAltitude methods.
}

3- As @sagi flagged, in the constructor, you're declaring and initializing another variable than in your class, but with the same name. Technically, doubles and integers are already initialized with 0.0 so you don't really notice it, but the variables altitude and totalAstronauts in your constructor are useless as they are. Suggesting to update the constructor like this:

   public SpaceStation(String name, double 
weight) {
this.name = name;
altitude = 0;    // Removed "int"
totalAstronauts = 0;  //Removed "int"
Astronauts = new Astronaut[3];
}

4- As @sagi flagged, you're re-initializing the Astronaut array every time you add an astronaut. You need to initialize it in the constructor once, and presumably set the astronauts in the array every time you add one.

With all of those comments, the SpaceStation class should look like this:

package gov.nasa.spacevehicles;
import gov.nasa.personnel.Astronaut;
public class SpaceStation {
//private members
private String name;
private double stationWeight;
private double altitude;
private Astronaut[] Astronauts;
private int totalAstronauts;
//overloaded constructor
public SpaceStation(String name, double 
weight) {
this.name = name;
altitude = 0;
totalAstronauts = 0; 
Astronauts = new Astronaut[3];
}
//method
public void addAstronaut(String name, double 
height, double weight) {
Astronauts[totalAstronauts] = new Astronaut(name, height, weight);
stationWeight = stationWeight + weight;
totalAstronauts++;  
}
public double setAltitude(double altitude) { return this.altitude = altitude; }
public String toString() {
return "SpaceStation: " + name + "\n" +
"Weight(kg): " + stationWeight + "\n" +
"Altitude(km): " + (altitude) + "\n" +
"Astronauts: " + (totalAstronauts);
}
public static void main(String[] args) {
SpaceStation ss = new SpaceStation("ISS", 419700.0);
ss.addAstronaut("Smith", 167.64, 81.65);
ss.addAstronaut("John", 185.43, 100.30);
ss.addAstronaut("Joey", 175.38, 90.38);
ss.setAltitude(400.0);
System.out.println(ss);
}
}

答案2

得分: 1

(1) SpaceStation的构造函数没有将传入的name和weight的值设置为SpaceStation对象的参数。在构造函数中添加this.name = namethis.stationWeight = weightthis关键字告诉Java,你引用的变量是构造函数所调用的对象的参数。

构造函数还创建了名为altitudetotalAstronauts变量,这些变量仅在构造函数内部存在。要改变构造函数所调用对象的参数值,将this.altitude = altitudethis.totalAstronauts = totalAstronauts添加到构造函数中。

//重载的构造函数
public SpaceStation(String name, double weight) {
      this.altitude = 0;
      this.totalAstronauts = 0;
      this.name = name;
      this.stationWeight = weight;
 }

(2) 在对ss执行操作之前,您的main方法会打印ss。将其放在主代码之后:

public static void main(String[] args) {
     SpaceStation ss = new SpaceStation("ISS", 419700.0);
     
     ss.addAstronaut("Smith", 167.64, 81.65);
     ss.addAstronaut("John", 185.43, 100.30);
     ss.addAstronaut("Joey", 175.38, 90.38);
     ss.setAltitude(400.0);

     System.out.println(ss);
}

(3) 每次调用addAstronaut()时,Astronauts数组都会被覆盖。通过在addAstronaut()中创建并添加新的Astronaut实例到Astronauts数组来修复这个问题。您应该在构造函数中添加一行代码,将数组初始化为空数组,大小为3或更大,以便在addAstronaut()中有空间添加Astronaut对象。

//重载的构造函数
public SpaceStation(String name, double weight) {
      this.altitude = 0;
      this.totalAstronauts = 0;
      this.name = name;
      this.stationWeight = weight;

      // 添加这一行
      Astronauts = new Astronaut[10];
}

//方法
public void addAstronaut(String name, double height, double weight) {
     
     // 在索引totalAstronauts处向数组添加一个新的Astronaut对象
     Astronauts[totalAstronauts] = new Astronaut(name, height, weight);
     
     stationWeight = stationWeight + weight;
     totalAstronauts++;  
}
英文:

(1) The constructor of SpaceStation does not set the passed in values of name and weight to the parameters of the SpaceStation object. Add this.name = name and this.stationWeight = weight to the constructor. The this keyword tells Java that the variable you are referring to is the parameter of the object that the constructor is being called for.

The constructor also creates new variables named altitude and totalAstronauts that exist only inside of the constructor. To change the values of the parameters of the object the constructor is called for, add this.altitude = altitude and this.totalAstronauts = totalAstronauts to the constructor.

//overloaded constructor
public SpaceStation(String name, double weight) {
      this.altitude = 0;
      this.totalAstronauts = 0;
      this.name = name;
      this.stationWeight = weight;
 }

(2) Your main method prints ss before you perform operations on it. Place it after the other code in your main:

public static void main(String[] args) {
     SpaceStation ss = new SpaceStation("ISS", 419700.0);
     
     ss.addAstronaut("Smith", 167.64, 81.65);
     ss.addAstronaut("John", 185.43, 100.30);
     ss.addAstronaut("Joey", 175.38, 90.38);
     ss.setAltitude(400.0);

     System.out.println(ss);
  }

(3) The Astronauts array is overwritten every time addAstronaut() is called. Fix this by creating and adding a new Astronaut instance to the Astronauts array in addAstronaut(). You should add a line to the constructor to initialize the array to an empty array of a size of 3 or greater so there is space to add Astronaut objects in addAstronaut().

//overloaded constructor
public SpaceStation(String name, double weight) {
      this.altitude = 0;
      this.totalAstronauts = 0;
      this.name = name;
      this.stationWeight = weight;

      // Add this line
      Astronauts = new Astronaut[10];
 }

//method
  public void addAstronaut(String name, double height, double weight) {
     
     // Adds a new Astronaut object to the array at index totalAstronauts
     Astronauts[totalAstronauts] = new Astronaut(name, height, weight);
     
     stationWeight = stationWeight + weight;
     totalAstronauts++;  
  }

答案3

得分: 0

有主要两个错误导致 name 被显示为 null。
1: 在你的构造函数中没有设置 name,这导致 name 有一个默认值为 null
因此当你说 this.name = name 时,它告诉我在我这里有这个东西,请查看并设置它的值。

//overloaded constructor
public SpaceStation(String name) {
    this.name = name; // 改变
    int altitude = 0;
    Astronauts = new Astronaut[3];
    int totalAstronauts = 0; 
}

此外,在 main 函数中,你在设置值之前尝试打印它。
你应该先给它赋值,然后再打印它。

public static void main(String[] args) {
    SpaceStation ss = new SpaceStation("ISS", 419700.0);
    
    ss.addAstronaut("Smith", 167.64, 81.65);
    ss.addAstronaut("John", 185.43, 100.30);
    ss.addAstronaut("Joey", 175.38, 90.38);
    ss.setAltitude(400.0);
    
    System.out.println(ss); // 改变
}

此外,我看到你创建了一个 Astronaut[] Astronauts 数组,但从未向其中添加过任何宇航员。那它的整个意义是什么?
当你添加宇航员时,它只存储了 astronauts 的总数,但从未实际存储 Astronauts 对象。

//method
public void addAstronaut(String name, double 
   height, double weight, Astronaut object) {
    Astronauts[totalAstronauts] = object; // 改变
    stationWeight = stationWeight + weight;
    totalAstronauts++;  
}

主函数:

public static void main(String[] args) {
    SpaceStation ss = new SpaceStation("ISS");
    
    ss.addAstronaut("Smith", 167.64, 81.65, new Astronaut("Smith", 167.64, 81.65));
    ss.addAstronaut("John", 185.43, 100.30, new Astronaut("John", 185.43, 100.30));
    ss.addAstronaut("Joey", 175.38, 90.38, new Astronaut("Joey", 175.38, 90.38));
    ss.setAltitude(400.0);
    System.out.println(ss);
    
    // (添加) 从数组中打印宇航员
    for(Astronaut a : ss.Astronauts) {
        System.out.println(a);
    }
}

Astronaut 类:

public class Astronaut {
    //private fields
    private String name;
    private double height;
    private double weight;

    //default constructor
    public Astronaut() { }

    //overloaded constructor
    public Astronaut (String name, double height, double weight) {
        this.name = name;
        this.height = height;
        this.weight = weight;
    } 

    // 添加 
    public String toString() {
        return "\nAstronaut Name: "+name+"\n"+name+" Height: "+height+"\n"+name+" Weight: "+weight;
    } 
}

输出:

SpaceStation: ISS
Weight(kg): 272.33
Altitude(km): 400.0
Astronauts: 3
Astronaut Name: Smith
Smith Height: 167.64
Smith Weight: 81.65
Astronaut Name: John
John Height: 185.43
John Weight: 100.3
Astronaut Name: Joey
Joey Height: 175.38
Joey Weight: 90.38
英文:

There are mainly 2 errors due to which name is been shown has null.<br>
1: You haven't set name in your constructor, Which resulted in name having a default value of null.<br>
So when you say this.name = name it tells that I have this in me, please look and set its value.

//overloaded constructor
public SpaceStation(String name) {
this.name = name; // changed
int altitude = 0;
Astronauts = new Astronaut[3];
int totalAstronauts = 0; 
}

Also in the main function, you were trying to print it before setting the values for it.
You should have first given the values for it and then print it.

public static void main(String[] args) {
SpaceStation ss = new SpaceStation(&quot;ISS&quot;, 419700.0);
ss.addAstronaut(&quot;Smith&quot;, 167.64, 81.65);
ss.addAstronaut(&quot;John&quot;, 185.43, 100.30);
ss.addAstronaut(&quot;Joey&quot;, 175.38, 90.38);
ss.setAltitude(400.0);
System.out.println(ss); // changed
}

Additionally, I see you have made an Astronaut[] Astronauts array but never added any Astronauts in it. So what's the whole point of it?
When you were adding Astronauts it only stored the total numbers of astronauts but never an actual Astronauts Object.<br>

   //method
public void addAstronaut(String name, double 
height, double weight, Astronaut object) {
Astronauts[totalAstronauts] = object; // changed
stationWeight = stationWeight + weight;
totalAstronauts++;  
}

Main:

public static void main(String[] args) {
SpaceStation ss = new SpaceStation(&quot;ISS&quot;);
ss.addAstronaut(&quot;Smith&quot;, 167.64, 81.65, new Astronaut(&quot;Smith&quot;, 167.64, 81.65));
ss.addAstronaut(&quot;John&quot;, 185.43, 100.30, new Astronaut(&quot;John&quot;, 185.43, 100.30));
ss.addAstronaut(&quot;Joey&quot;, 175.38, 90.38, new Astronaut(&quot;Joey&quot;, 175.38, 90.38));
ss.setAltitude(400.0);
System.out.println(ss);
// (Added) Printing astronauts from array
for(Astronaut a : ss.Astronauts) {
System.out.println(a);
}
}

Astronaut class:

public class Astronaut {
//private fields
private String name;
private double height;
private double weight;
//defualt constructor
public Astronaut() { }
//overloaded constructor
public Astronaut (String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
} 
// Added 
public String toString() {
return &quot;\nAstronaut Name: &quot;+name+&quot;\n&quot;+name+&quot; Height: &quot;+height+&quot;\n&quot;+name+&quot; Weight: &quot;+weight;
} 
}

Output:

SpaceStation: ISS
Weight(kg): 272.33
Altitude(km): 400.0
Astronauts: 3
Astronaut Name: Smith
Smith Height: 167.64
Smith Weight: 81.65
Astronaut Name: John
John Height: 185.43
John Weight: 100.3
Astronaut Name: Joey
Joey Height: 175.38
Joey Weight: 90.38

huangapple
  • 本文由 发表于 2020年9月28日 10:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64095304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定