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

09-04 20:33
查看更多