首先,作为提示,添加方法标头很有用,这样您就知道您的方法要做什么。Aka,如果您查看您的规范,您的许多方法都要求您“知道多少......”所以这些方法应该返回一个数字,而不是立即打印出一些东西(我在开始编码时犯了同样的错误)。
您正在您的方法中打印出这些数字(这对调试很有用,但不是您的成品应该做什么)。您可以返回一个 int,然后在 RunSugarBowl 中打印出该整数(见下文)。
我已经为您提供了一个总体框架,并添加了一些可能对您有所帮助的评论。你一开始就做得很好。如果您有更多问题,请在评论中提问。
public class SugarBowl {
private int totalCapacity;
private int availableSpace;
private int spoonSize;
private int occupiedSpace;//starts at 0, because there's nothing in the bowl.
/**
* Constructor for the sugar bowl.
* @param totalCapacity The total capacity of the bowl.
*/
public SugarBowl (int totalCapacity){
this.totalCapacity = totalCapacity; //set the totalCapacity for the bowl
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
occupiedSpace = 0;
}
/**
* Shows how much sugar can fit in a spoon.
* @return The size of the spoon
*/
public int spoon(){
return spoonSize;
}
/**
* Returns amount of sugar in the bowl.
* @return The amount of occupied space
*/
public int occupied(){
return occupiedSpace;
}
/**
* Removes the amount of sugar based on the
* number of scoops passed into it.
* @param numberOfScoops The number of scoops
* @return The amount of sugar removed
*/
public int scoops (int numberOfScoops){
int possibleAmountTaken = numberOfScoops * spoonSize;
int actualAmountTaken = 0;
//Think about sugar, even if there is less sugar than the spoon size,
//can a spoon still remove that amount?
//aka the only time 0 sugar should be taken is when there is 0 sugar in the bowl
if (possibleAmountTaken<=occupiedSpace){
actualAmountTaken = possibleAmountTaken;
}
else{
//there may still be sugar, just not enough for every scoop, you still have to remove it
//actualAmountTaken = ???
}
occupiedSpace = occupiedSpace - actualAmountTaken;
//what about availableSpace?
//availableSpace = ???
return actualAmountTaken;
}
/**
* Adds the specified amount of sugar to the bowl.
*
* @param addedAmount The amount of sugar added to the bowl
* @return The overflow amount of sugar or 0 if there was no overflow
*/
public int addSugar (int addedAmount){
int overflow = 0;
if (addedAmount>availableSpace){
overflow = addedAmount-availableSpace;
//your bowl is going to be full, what happens to occupiedSpace and availableSpace?
//availableSpace = ???
//occupiedSpace = ???
}
else{
//overflow is already 0 so you don't have to do anything with it
//update availableSpace and occupiedSpace
//availableSpace = ???
//occupiedSpace = ???
}
return overflow;
}
}
用你上面的主要例子:
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
System.out.println("Sugar overflow: " + Integer.toString(one.addSugar(300))); //once working correctly should print out 100 for the overflow
System.out.println("Occupied size is : "+ one.occupied());
}
}
更新
您使用 this.totalCapacity 的原因是因为以下代码行:
public class SugarBowl {
private int totalCapacity; //totalCapacity for this object; aka this.totalCapacity refers to this variable
//..
public SugarBowl (int totalCapacity){ // <-- totalCapacity passed in
this.totalCapacity = totalCapacity; //this.totalCapacity is setting the totalCapacity for this instance of the object to the value passed in
//..
请注意您的构造函数是如何在名为“totalCapacity”的变量中传递的,但该类也有自己的内部变量,称为totalCapacity。没有“this”关键字的可比代码如下:
public class SugarBowl {
private int bowlTotalCapacity; //totalCapacity for this object
//..
public SugarBowl (int totalCapacity){
bowlTotalCapacity = totalCapacity;
//..
您只需确保在初始化之前使用过 totalCapacity 的任何位置后,将其更改为 bowlTotalCapacity。使用 this.totalCapacity 来引用此类中的 totalCapacity 要容易得多。在这里查看更多信息:http ://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html 。
从技术上讲,您在最初构造对象后实际上并没有再次使用 totalCapacity,但是如果您想查看如果不包含此部分会发生的奇怪情况,请尝试了解以下代码中发生的情况:
public class ThisExample {
private int wrongExample = 0;
private int thisExample = 0;
public ThisExample (int wrongExample, int thisExample){
wrongExample = wrongExample;
this.thisExample = thisExample;
}
public int getThisExample(){
return thisExample;
}
public int getWrongExample(){
return wrongExample;
}
}
运行以下命令可以帮助您更好地理解:
public class ThisExampleMain {
public static void main(String[] args) {
ThisExample ts = new ThisExample(50, 50);
//you want this to be 50 but it ends up being 0:
System.out.println("Wrong: " + ts.getWrongExample());
//this returns the correct answer:
System.out.println("Right: " + ts.getThisExample());
}
}