如何使用Java ArrayList获得字符串用户输入并增加 1的值



我有以下代码,我想在已经存储在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;
}

您在哪里增加年龄?按照您在 dog class中定义的方法调用 newage()方法。`

     Dog dog = dogs.get(i);
     dog.setAge(dog.newAge());
     dogs.set(i,dog);`

我们取了一条符合细节的特定狗。然后,我们将其年龄增长,并将其设置在该位置的列表中。

相关内容

  • 没有找到相关文章

最新更新