Java初学者:类和方法
我是noob,Java是我的第一种编程语言。 我有一个我正试图解决的问题: 定义一个类来表示以三个 物品:总容量(克)、可用数量和 勺子(一个勺子需要多少克糖 它)。在该类中设置:Java初学者:类和方法,java,Java,我是noob,Java是我的第一种编程语言。 我有一个我正试图解决的问题: 定义一个类来表示以三个 物品:总容量(克)、可用数量和 勺子(一个勺子需要多少克糖 它)。在该类中设置: 用于创建糖碗的构造函数方法 一种可以知道糖碗中糖量的方法 一种可以知道一勺糖需要多少的方法 一种方法,给定一定数量的勺子,除去相应数量的糖,并返回该值 加糖的方法。如果提供的值超过可用空间,则糖罐应满,剩余量 返回。在其他情况下,应返回零 将classRunSugarBowl设置为main并进行游戏。 你应该替换
RunSugarBowl
设置为main并进行游戏。
你应该替换
sugarBowl (int totalCapacity){
availableSpace=totalCapacity;
spoonSize = totalCapacity/20;//arbitrary size
}
借
您可以将main()
方法添加到sugarBowl
类中。运行它不需要单独的类
我将修改您的主视图,如下所示:
public class RunSugarBowl {
public static void main(String[] args) {
sugarBowl one = new sugarBowl(200);
one.occupied();
one.addSugar(100);
}
}
或者您可以将类及其构造函数重命名为
SugarBowl
。我推荐第二种方法,因为它符合Sun Java编码标准。您的主方法调用这些方法,但不使用从这些方法返回的值。
您应该更改main
方法,如下所示
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
System.out.println("Occupied size is : "+ one.occupied());
}
}
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.
}
}
按如下所示更改addSugar()方法
public class RunSugarBowl {
public static void main(String[] args) {
SugarBowl one = new SugarBowl(200);
System.out.println("Occupied size is : "+ one.occupied());
}
}
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.
}
}
添加sugar时没有更新occupiedSpace字段。这就是为什么它总是保持不变的原因。不幸的是,您的代码错误。您应该更改此函数
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");}
}
与
因为你要建造一个容量为200的糖碗,然后再加上100。所以
if (addedAmount>availableSpace)
100>200返回false,它直接进入else块,该块只打印出“0”。这就是你犯的逻辑错误。但别担心,我们都去过那里;)
实际上是线路
occupiedSpace=可用总容量空间代码>
是必要的吗
availableSpace=availableSpace-addedAmount代码>
因为您使用了占用空间作为属性。在类开头编写的公式仅在调用构造函数时执行一次(由于它们是基元int,所以最初两个值都是0),并且占用空间保持为0,除非通过其他函数调用对其进行更改
但是,如果希望每次需要时计算占用空间或可用空间,则应删除其中一个,因此只需要另一个和总容量。另一个可以通过从总容量中减去一次来计算。如果我是你,我会用
public int getOccupiedSpace(){
return totalCapacity-availableSpace;
}
而不是使用(顺便说一下,这是您案例中的useles)
这是一样的
private int occupiedSpace = 0;
或
首先,作为提示,添加方法头非常有用,这样您就可以知道您的方法试图做什么。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;
}
}
更新
使用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,都会将其更改为bowlTotalCapacity。使用this.totalCapacity来引用这个类中的totalCapacity要简单得多。有关详细信息,请查看此处:
从技术上讲,在最初构建对象之后,您不会再实际使用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());
}
}
规范要求的addSugar方法应返回int:
addSugar方法还应更新availableSpace和occupiedSpace
基本上,您正在调用一个.occulated()方法,但您没有使用该值执行任何操作,也许您希望打印它以查看它
以下是您的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());
}
}到目前为止,您尝试了什么?您是否编写了整个程序而没有运行它?您定义了sugarBowl
,但您正在创建一个sugarBowl
。尝试更正你的大写字母(在Java中,类名应该大写,所以我会在那里这样做)并再次运行。我添加了将程序翻译成英语的内容,即sugarBowl和sugarBowl不是原因,因为它不在原始版本中。谢谢你,我已经编辑过了。只要你把这个班级改名为SugarBowl;正如所写的那样,它是正确的。但是如果我尝试:“public class RunSugarBowl{public static void main(String[]args){SugarBowl one=new SugarBowl(200);one.occulated();”它不会返回任何结果,似乎我没有足够的声望去投票(我是网站和满足系统的新手。但是我接受了。现在我得到了。这个问题得到了纠正。但是我的占用方法在添加300克后仍然打印0(通过'one.addSugar(300)')。为什么?因为你没有在糖可用时添加糖。当可以添加糖时,你的代码只打印0。你应该在else部分中进行修复。你能更清楚地解释一下吗(对不起,英语不是。)
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());
}
}
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
//..
public class SugarBowl {
private int bowlTotalCapacity; //totalCapacity for this object
//..
public SugarBowl (int totalCapacity){
bowlTotalCapacity = 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());
}
}
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());
}