英文:
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 < i; j++){
System.out.println(person[j])
}
Note: Make sure to change the while (flag)
to while(flag && i < 100)
to avoid ArrayIndexOutOfBoundsException
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论