怎么按顺序打印出来?

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

How can I print it out in order?

问题

我使用对象和覆盖,但我不喜欢打印出来的结果。

我想要的结果是...

示例:
1:创建薪水对象,2:创建小时对象,3:退出 --> 1

姓名,身份证号码,月薪:

A 2345 3000000

我该如何修复这段代码?

英文:

I use object and override, but I don't like the printed result

The result I want is...

ex)
1: Salaried object creation, 2: Hour object creation 3. Exit --> 1

name, id number, monthly salary : 

A 2345 3000000



How do I fix the code?


</details>


# 答案1
**得分**: 1

## 问题

您没有将 `SalariedEmployee` 和 `HourEmployee` 对象分配给您的 `Person[]`。

## 解决方案

在您的 `while` 循环之外创建一个计数器变量,例如 `int i = 0;`,然后,

将

```java
salary = new SalariedEmployee(name1, id1, salariedemployee);

替换为

person[i++] = new SalariedEmployee(name1, id1, salariedemployee);

以及

hoem = new HourEmployee(name2, id2, houremployee, hour);

替换为

person[i++] = new HourEmployee(name2, id2, houremployee, hour);

最后,在 case 3: 中迭代 Person[],例如:

for (int j = 0; j < i; j++) {
    System.out.println(person[j]);
}

注意: 确保将 while (flag) 更改为 while (flag && i < 100),以避免出现 ArrayIndexOutOfBoundsException

英文:

Problem

You are not assigning the SalariedEmployee and HourEmployee objects to your Person[].

Solution

Create a counter variable e.g. int i = 0; outside your while loop. Then,

Replace

salary = new SalariedEmployee(name1, id1, salariedemployee);

with

person[i++] = new SalariedEmployee(name1, id1, salariedemployee);

and

hoem = new HourEmployee(name2, id2, houremployee, hour);

with

person[i++] = new HourEmployee(name2, id2, houremployee, hour);

Finally, iterate the Person[] in case 3: e.g. as follows:

for(int j = 0; j &lt; i; j++){
    System.out.println(person[j])
} 

Note: Make sure to change the while (flag) to while(flag &amp;&amp; i &lt; 100) to avoid ArrayIndexOutOfBoundsException.

huangapple
  • 本文由 发表于 2020年9月26日 21:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64077981.html
匿名

发表评论

匿名网友

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

确定