2016-12-09 105 views
0

我有下面的代碼,我想問一下已經存儲在Arraylist中的名字,並且增加(狗)年齡+1。如何獲得字符串用戶輸入並使用Java 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); 
    } 
} 
+0

問題是什麼?任何錯誤消息? – Robert

+0

我記得幾天前的這個問題,也許你需要更好地搜索。 – Robert

回答

1

所以在這個代碼你找到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; 
} 
0

你在哪裏增加年齡?調用NEWAGE如您在 class.`

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

已經定義了我們獲取誰的細節匹配特定的狗()方法。然後我們增加它的年齡並將它放回到該位置的列表中。

相關問題