gpt4 book ai didi

java - 如何判断用户输入的容量是否大于当前容量?

转载 作者:行者123 更新时间:2023-12-01 11:41:17 24 4
gpt4 key购买 nike

我目前正在开发一个与狗一起使用的项目,我当前正在开发的方法应该让用户更改狗窝容量。下面的代码来自狗窝构造函数类:

public class Kennel {
private String name;
private ArrayList < Dog > dogs;
private int nextFreeLocation;
private int capacity;

/**
* Creates a kennel with a default size 20
*
* @param maxNoDogs
* The capacity of the kennel
*/
public Kennel() {
this(20);
}

/**
* Create a kennel
*
* @param maxNoDogs
* The capacity of the kennel
*/
public Kennel(int maxNoDogs) {
nextFreeLocation = 0; // no Dogs in collection at start
capacity = maxNoDogs;
dogs = new ArrayList < Dog > (capacity); // set up default. This can
// actually be exceeded
// when using ArrayList but we
// won't allow that
// to happen.
}

/**
* This method sets the value for the name attribute. The purpose of the
* attribute is: The name of the kennel e.g. "DogsRUs"
*
* @param theName
*/
public void setName(String theName) {
name = theName;
}

/**
* Set the size of the kennel
*
* @param capacity
* The max dogs we can house
* @return
*/

public void setCapacity(int capacity) {
if (this.capacity > capacity) {
this.capacity = capacity;
} else
System.out.println("The capacity you entered is lower than the current capacity");
}

/**
* Maximum capacity of the kennels
*
* @return The max size of the kennel
*/
public int getCapacity() {
return capacity;
}

/**
* This method gets the value for the name attribute. The purpose of the
* attribute is: The name of the Kennel e.g. "DogsRUs"
*
* @return String The name of the kennel
*/
public String getName() {
return name;
}

/**
* This method returns the number of dogs in a kennel
*
* @return int Current number of dogs in the kennel
*/
public int getNumOfDogs() {
return nextFreeLocation;
}

/**
* Enables a user to add a Dog to the Kennel
*
* @param theDog
* A new dog to home
*/
public void addDog(Dog theDog) {
if (nextFreeLocation >= capacity) {
System.out.println("Sorry kennel is full - cannot add team");
return;
}
// we add in the position indexed by nextFreeLocation
// This starts at zero
dogs.add(theDog);

// now increment index ready for next one
nextFreeLocation = nextFreeLocation + 1;
}

/**
* Enables a user to delete a Dog from the Kennel.
*
* @param theDog
* The dog to remove
*/
public void removeDog(String who) {
Dog which = null;
// Search for the dog by name
for (Dog d: dogs) {
if (who.equals(d.getName())) {
which = d;
}
}
if (which != null) {
dogs.remove(which); // Requires that Dog has an equals method
System.out.println("removed " + who);
nextFreeLocation = nextFreeLocation - 1;
} else
System.err.println("cannot remove - not in kennel");
}

/**
* @return String showing all the information in the kennel
*/
public String toString() {
StringBuilder sbr = new StringBuilder();
sbr.append("Data in Kennel " + name + " is:");
for (Dog d: dogs) {
sbr.append(d.toString() + "\n");
}
return sbr.toString();
}

/**
* Returns an array of the dogs in the kennels
*
* @return An array of the correct size
*/
public Dog[] obtainAllDogs() {
Dog[] result = new Dog[dogs.size()];
result = dogs.toArray(result);
return result;
}

/**
* Only returns those dogs who like bones
*
* @return An array of dogs of the correct size. If no dogs like bones then
* returns an empty array (size 0)
*/
public List < Dog > obtainDogsWhoLikeBones() {
List < Dog > result = new ArrayList < Dog > (); // changed to List<dog> so can
// freely add data without
// intializing to fixed size
for (Dog d: dogs) {
if (d.getLikesBones()) {
result.add(d);
}
}
return result;
}

public Dog search(String name) {
Dog result = null;
for (Dog d: dogs) {
if (name.equals(d.getName())) {
result = d;
}
}
return result;
}

}

这个方法来自狗窝应用程序类:

private void setKennelCapacity() {
System.out.print("Enter max number of dogs: ");
int max = scan.nextInt();
scan.nextLine();
kennel.setCapacity(max);
System.out.println(max + " " + kennel.getCapacity());
}

结果是运行else语句,总是说输入的数字太小。这可能是一个小错误,但我缺乏经验,感谢您的帮助。

最佳答案

usercapacity 是一个局部变量。每次调用setCapacity(int)时,变量usercapacity将被初始化为0(该变量将始终为0)。相反,创建一个字段变量来存储容量

class Kernal {
private int capacity;

public void setCapacity(int newCapacity) {
if(newCapacity < capacity) {
throw new IllegalArgumentException("You cannot enter a value that is lower than the current capacity");
}

capacity = newCapacity;
}
}
<小时/>

如果没有IllegalArgumentException(使用简单的打印语句来通知用户),您的方法将如下所示:

public void setCapacity(int capacity) {
if (this.capacity < capacity) {
this.capacity = capacity;
} else
System.out.println("The capacity you entered is lower than the current capacity");
}

关于java - 如何判断用户输入的容量是否大于当前容量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29502183/

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