gpt4 book ai didi

Java 初学者 : classes and methods

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:13:05 24 4
gpt4 key购买 nike

我是菜鸟,Java 是我的第一门编程语言。我有这个问题,我正在努力解决:

Define a class to represent a Sugar Bowl sugar characterized by three things: total capacity (in grams), available quantity and quantity of the spoon (how many grams of sugar takes the spoon associated with it). Set in this class:

  1. A constructor method to create a sugar bowl.
  2. A method that allows to know the amount of sugar in the sugar bowl.
  3. A method that allows to know how much it takes a spoonful of sugar.
  4. A method which, given a number of scoops, removes the corresponding amount of sugar, returning this value.
  5. A method to add sugar. If the supplied value exceeds the available space, the sugar bowl should be full, and the remaining amount returned. In other cases, should return zero.

Set the class RunSugarBowl as main and play around.

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 in:

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());
}
}

最佳答案

很遗憾,您的代码有误。你应该改变这个功能

    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);
}
}

因为您 build 了容量为 200 的糖 jar ,稍后再添加 100。所以

if (addedAmount>availableSpace) 

100 > 200 返回 false 并直接转到 else block ,该 block 仅打印出“0”。这是你犯的逻辑错误。但别担心,我们都去过那里;)

其实行 occupiedSpace = totalCapacity-availableSpace; 是必不可少的 availableSpace = availableSpace - addedAmount;因为您使用 occupiedSpace 作为属性。您在类(class)开头编写的公式仅执行一次,当您调用构造函数时(最初您提取的两个值都是 0,因为它们是原始 int)并且 occupiedSpace 保持为 0 除非您使用其他函数调用更改它。

但是,如果您希望在每次需要时计算occupiedSpaceavailableSpace,您应该删除其中一个,因此您只需要另一个和<强>总容量 。其他的可以每次从totalCapacity中减去一个来计算。如果我是你,我会使用

public int getOccupiedSpace(){
return totalCapacity-availableSpace;
}

而不是使用(这对你的情况有用)

private int occupiedSpace = totalCapacity-availableSpace;

这是一样的

private int occupiedSpace = 0;

private int occupiedSpace ;

关于Java 初学者 : classes and methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14059274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com