大家下午好。在工作和 JavaRush 之间,我购买并开始阅读 Kathy Sierra 和 Bert Bates 所著的《学习 Java》。今天我又做了课本上的作业。我无法解决第 95 页的“ class Triangle ”问题。在 IntelliJ IDEA 中,答案不是我需要的。我花了两个小时,尝试了所有可能的选择,但没有任何效果。他吐了口唾沫,然后去找答案。这是我的第一个决定。结果,如果你把这段代码输入IntelliJ IDEA,答案将不是课本上写的。 答案中的正确解决方案如下所示:
public class Triangle
{
double area;
int height;
int lenght;
public static void main(String[] args)
{
int x = 0;
Triangle [] ta = new Triangle[4];
while (x < 4)
{
ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].lenght = x + 4;
ta[x].setArea();
System.out.print("треугольник " +x+ ", зона");
System.out.println(" = " + ta[x].area);
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print("y = " + y);
System.out.println(", зона t5 = " + t5.area);
}
void setArea()
{
area = (height + lenght) / 2;
}
}
根据教科书作者的说法,答案应该是:
треугольник 0, зона = 4.0
треугольник 1, зона = 10.0
треугольник 2, зона = 18.0
треугольник 3, зона = 28.0
y = 4, зона t5 = 343.0
事实上,答案是这样的:
треугольник 0, зона = 3.0
треугольник 1, зона = 4.0
треугольник 2, зона = 6.0
треугольник 3, зона = 7.0
y = 4, зона t5 = 343.0
我只有两个可能的答案:
-
技术不完整和拼写错误。
-
这本书是用Java 5.0编写的,我在Java 8.0中解决了这个问题
GO TO FULL VERSION