gpt4 book ai didi

java - 如何使用 Java ArrayList 获取字符串用户输入并将值增加 +1?

转载 作者:行者123 更新时间:2023-12-01 09:08:32 26 4
gpt4 key购买 nike

我有以下代码,我想询问用户已存储在 Arraylist 中的名称,并将该(狗)年龄增加 +1。

这是代码中最重要的(我猜)部分。这是寄存器类。

import java.util.Scanner;
import java.util.ArrayList;

public class Register {

private Scanner keyboard = new Scanner(System.in);
private ArrayList<Dog> dogs = new ArrayList<>();


private String readString() {
return keyboard.nextLine();
}

public int readInt() {
int i = keyboard.nextInt();
keyboard.nextLine();
return i;
}

public double readDouble() {
double d = keyboard.nextDouble();
keyboard.nextLine();
return d;
}


public void registrateDog(){

System.out.println("Dogs name: ");
String name = readString();

System.out.println("Dogs breed: ");
String breed = readString();

System.out.println("Dogs age: ");
int age = readInt();

System.out.println("Dogs weight: ");
double weight = readDouble();

Dog newDog = new Dog(name, breed, age, weight);
dogs.add(newDog);

System.out.println(newDog.toString() + " is added");

}



public void increaseAge(){ //Here's the problem

System.out.print("Enter dog who has aged: ");
String newDogAge = readString();

int addAge = 1;

for (int i = 0; i < dogs.size(); i++) {
if(dogs.get(i).getName().equals(newDogAge))

addAge = (dogs.get(i).getAge());
dogs.set(i, dogs.get(i));


System.out.println("Dog " + newDogAge + " is now " + addAge);
return;

}
}

private void exitProgram(){
System.out.println("Goodbye!");
keyboard.close();


}

private void run(){
setUp();
runCommandLoop();
exitProgram();

}

public static void main(String[] args){

new Register().run();
//setUp();
}


}

还有

public class Dog {

private String name;
private String breed;
private int age;
private double weight;
private double tailLenght;
private String tax = "tax";

public Dog(String name, String breed, int age, double weight){
this.name = name;
this.breed = breed;
this.age = age;
this.weight = weight;
if (breed.equals(tax)) {
this.tailLenght = 3.7;
} else {
this.tailLenght = (age*weight) / 10;
}
}



public String getName(){
return name;
}

public void setName(String name) {
this.name = name;
}

public String getBreed(){
return breed;
}

public int getAge(){
return age;
}

public void setAge(int age){
this.age = age;
}

public int newAge(){
return age = age + 1;
}



public double getWeight(){
return weight;
}

public double getTailLenght(){
return tailLenght;
}

public String toString()
{ return String.format("%s, %s, %d years old, %.1f kg, "
+ "tail lenght %.1f cm", name, breed, age, weight, tailLenght);
}
}

最佳答案

因此,在这段代码中,您可以通过名称找到Dog。这太棒了。

public void increaseAge() {
System.out.print("Enter dog who has aged: ");
String newDogAge = readString();
int addAge = 1;

for (int i = 0; i < dogs.size(); i++) {
if(dogs.get(i).getName().equals(newDogAge))
addAge = (dogs.get(i).getAge());
dogs.set(i, dogs.get(i));
System.out.println("Dog " + newDogAge + " is now " + addAge);
return;
}
}

在您的 Dog 类中,您有一个返回年龄的方法,您要做的是将其修改为:

public int newAge(){
this.age++; // This will take the current age of the dog and add one to it
}

现在回到 increaseAge() 方法,您希望将 for 循环修改为如下所示:

for (int i = 0; i < dogs.size(); i++) { 
if(dogs.get(i).getName().equals(newDogAge))
dogs.get(i).newAge(); // This will update the Dogs age by 1 year
System.out.println("Dog " + dogs.get(i).getName() + " is now " + dogs.get(i).getAge());
return;
}

关于java - 如何使用 Java ArrayList 获取字符串用户输入并将值增加 +1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41066570/

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