有人可以向我解释以下问题吗?我不是在找你为我解答,而是让你向我解释,这样我才能朝着正确的方向前进。我一直在这堂课中苦苦挣扎......我自学了html和css,可以像我的手背一样流利地写出来。所以,当我报名参加本课程时,我认为我也很容易接受java ...我错了:)
public class TestGlass
{
public static void main(String [] args)
{
Glass milk = new Glass(15); // 15 ounces of milk
Glass juice = new Glass(3); // 3 ources of juice
milk.drink(2);
milk.drink(1);
milk.report();
juice.fill(6); // went from 3 to 9 ounces
juice.drink(1); // now down to 8 ounces
juice.report();
juice.spill();
juice.report();
}
}
class Glass
{
// Declare a variable to keep track of the number of ounces in a glass here
// Write the methods here. You will need a constructor, plus the drink, report, fill,
// and spill methods. These methods are for any generic glass -- they are not specific
// to the milk or juice instances of a glass (i.e., you should not mention "juice" or
// "milk" inside the Glass class).
}
这就是输出看起来像......
玻璃有12盎司 玻璃有8盎司 玻璃有0盎司。
答案 0 :(得分:1)
看看教授为您帮助插入的评论栏:
// Declare a variable to keep track of the number of ounces in a glass here
// Write the methods here. You will need a constructor, plus the drink, report, fill,
// and spill methods. These methods are for any generic glass -- they are not specific
// to the milk or juice instances of a glass (i.e., you should not mention "juice" or
// "milk" inside the Glass class).
看起来你应该:
写一个:
drink
方法从盎司数减去传递给方法的盎司数。fill
方法添加传递给盎司数的盎司数。spill
方法将盎司数设置为0,因为它全部溢出。 report
方法:
Glass has <number of ounces> ounces.
其中<number of ounces>
是变量的盎司数。
之后,你就完成了。
答案 1 :(得分:0)
您应该编写Glass类的构造函数和方法,以便测试编译,运行并生成预期的输出。这是测试驱动开发的一个示例 - 首先编写测试,然后编写测试对象的实现。