我是菜鸟,Java是我的第一个编程语言。 我有这个问题,我正在努力解决:
定义一个类来表示以三个为特征的糖碗糖 东西:总容量(克),可用数量和数量 勺子(多少克糖与勺子相关 它)。在这个课程中设置:
- 创建糖碗的构造方法。
- 一种可以知道糖碗中糖含量的方法。
- 一种方法,可以知道一勺糖需要多少。
- 一种方法,给定一些勺子,去掉相应数量的糖,然后返回这个值。
- 添加糖的方法。如果提供的值超出可用空间,则糖碗应该已满,剩余量 回。在其他情况下,应该返回零。
醇>将班级
RunSugarBowl
设为主要游戏。
public class SugarBowl {
private int totalCapacity;
private int availableSpace;
private int spoonSize;
private int occupiedSpace = totalCapacity-availableSpace;//is the same as amount of sugar in the bowl.
SugarBowl (int totalCapacity){
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
}
public int spoon(){
return spoonSize;
}
public int occupied(){
return occupiedSpace;
}
public void scoops (int numberOfScoops){
int amountTaken = numberOfScoops * spoonSize;
if (amountTaken<=occupiedSpace){
occupiedSpace=occupiedSpace-amountTaken;
System.out.println(amountTaken);}
else{
System.out.println("There's not that amount of sugar in the sugar bowl. Try less.");}
}
public int addSugar (int addedAmount){
if (addedAmount>availableSpace){
int remainingAmount=addedAmount-availableSpace;
availableSpace=0;
occupiedSpace = totalCapacity-availableSpace;
return remainingAmount;}
else{
availableSpace = availableSpace - addedAmount;
occupiedSpace = totalCapacity-availableSpace;
return 0;}
}
}
我的问题现在是我的one.occupied
方法返回0
而不是200
:
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
one.addSugar(300);
System.out.println("Occupied size is : "+ one.occupied());
}
}
答案 0 :(得分:3)
遗憾的是,您的代码错误。你应该改变这个功能
public void addSugar (int addedAmount){
if (addedAmount>availableSpace){
int remainingAmount=addedAmount-availableSpace;
System.out.println("Sugar bowl completely full. You got left: "+ remainingAmount);}
else{
System.out.println("0");}
}
与
public void addSugar (int addedAmount){
if (addedAmount>availableSpace){
System.out.println("Sugar bowl completely full. You got left: "+ remainingAmount);}
else{
availableSpace = availableSpace - addedAmount; //this ensures available space changes after you add sugar
occupiedSpace = totalCapacity-availableSpace; // you must also write this to change the ocuuppied space too
System.out.println(availableSpace);
}
}
因为你构造了200容量的糖碗,并在以后添加100。所以
if (addedAmount>availableSpace)
100&gt; 200返回false并直接进入else块,该块只打印出'0'。这是您犯的逻辑错误。但不要担心我们都去过那里;)
其实这一行
occupiedSpace = totalCapacity-availableSpace;
是必不可少的
availableSpace = availableSpace - addedAmount;
因为您使用 occupiedSpace 作为属性。你在类的开头写的公式只执行一次,当你调用构造函数时(最初两个值都是0,因为它们是原始的int)而 occupiedSpace 保持为 0 除非你用其他函数调用更改它。
但是如果你想在每次需要时计算 occupSpace 或 availableSpace ,你应该删除其中一个,因此你只需要另一个和<强> totalCapacity 即可。可以通过从 totalCapacity 中减去一个来计算其他一个。如果我是你,我会用
public int getOccupiedSpace(){
return totalCapacity-availableSpace;
}
而不是使用(在你的情况下使用的方式)
private int occupiedSpace = totalCapacity-availableSpace;
这是相同的
private int occupiedSpace = 0;
或
private int occupiedSpace ;
答案 1 :(得分:2)
首先,就像提示一样,添加方法标题非常有用,这样您就可以知道您的方法正在尝试做什么。 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,但是如果你想看到如果你不包含这个部分会发生的怪异,试着去理解下面代码中会发生什么: / p>
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());
}
}
答案 2 :(得分:2)
规范要求的addSugar方法应该返回一个int: addSugar方法还应该同时更新availableSpace和occupiedSpace。
在main中你正在调用one.occupied()方法,但你没有对该值做任何事情,也许你想打印它来看它。
以下是您的addSugar方法和主要方法。
public int addSugar (int addedAmount){
if (addedAmount>availableSpace){
int remainingAmount=addedAmount-availableSpace;
return remainingAmount;
}
else
{
availableSpace = availableSpace - addedAmount;
occupiedSpace = occupiedSpace + addedAmount;
return 0;
}
}
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
System.out.println("The occupied Space is: " + one.occupied());
System.out.println("The addSugar() method is returning " + one.addSugar(300));
System.out.println("The occupied Space now is: " + one.occupied());
}
}
答案 3 :(得分:1)
您的main方法调用方法但不使用从这些方法返回的值。
您应该按如下方式更改main
方法。
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
System.out.println("Occupied size is : "+ one.occupied());
}
}
按如下方式更改addSugar()方法。
public void addSugar (int addedAmount){
if (addedAmount>availableSpace){
int remainingAmount=addedAmount-availableSpace;
availableSpace=0;
//Update occupiedSpace here.
System.out.println("Sugar bowl completely full. You got left: "+ remainingAmount);}
else{
availableSpace = availableSpace - addedAmount; //this ensures available space changes after you add sugar
System.out.println("I added "+addedAmount + "to the bowl");
// Update occupiedSpace here.
}
}
当您添加糖时,您没有更新您的occupSpace字段。这就是它始终保持相同的原因。
答案 4 :(得分:0)
你应该替换
sugarBowl (int totalCapacity){
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
}
通过
public SugarBowl (int totalCapacity){
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
}
答案 5 :(得分:0)
您可以将main()
方法添加到sugarBowl
课程中。您不需要单独的类来运行它。
我会修改你的主要内容:
public class RunSugarBowl {
public static void main(String[] args) {
sugarBowl one = new sugarBowl(200);
one.occupied();
one.addSugar(100);
}
}
或者您可以将您的类及其构造函数重命名为SugarBowl
。我建议使用第二个,因为它与Sun Java编码标准一致。