我有3只狗的Arraylist
,我可以在列表中註冊更多的狗,通過調用toString
函數列出它們,將狗的年齡增加1,並從列表中移除狗。只要你只使用最後兩個,remove()
和increaseAge()
它們都可以工作,但只要你使用任何其他的命令,它們就不會找到任何狗,併發送回-1,因爲它不應該按名稱找到任何狗。所以它是remove()
和increaseAge()
這很奇怪。什麼使這些功能在第一次輸入後不起作用?
主要代碼:
public class DogTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
ArrayList<Dog> Dogs = new ArrayList<Dog>();
DogFunctions DF = new DogFunctions();
Dogs.add(new Dog("Peggy", "Labrador", 9, 30));
Dogs.add(new Dog("Max", "Tax", 4, 13));
Dogs.add(new Dog("Sanna", "Schäfer", 7, 25));
Boolean loop = true;
while(loop){
String input = keyboard.nextLine();
switch (input){
case "Register":
Dogs.add(DF.register());
break;
case "IncreaseAge":
Dogs.get(DF.returnDogIndex(Dogs, "Input Message", "Output Message")).increaseAge();
break;
case "List":
DF.list(Dogs);
break;
case "Remove":
Dogs.remove(DF.returnDogIndex(Dogs, "Input Message", "Output Message"));
break;
case "Quit":
loop = false;
break;
}
}
keyboard.close();
}
}
代碼的功能:
import java.util.ArrayList;
import java.util.Scanner;
public class DogFunctions {
private Scanner keyboard = new Scanner(System.in);
public Dog register(){
System.out.println("Namn:");
String name = keyboard.nextLine();
System.out.println("Ras:");
String breed = keyboard.nextLine();
System.out.println("Ålder:");
int age = keyboard.nextInt();
System.out.println("Vikt:");
double weight = keyboard.nextDouble();
Dog d = new Dog(name, breed, age, weight);
System.out.println("Hund tillagd i registret");
return d;
}
public void list(ArrayList<Dog> Dogs){
System.out.println("Ange svanslängd:");
double input = keyboard.nextDouble();
if(input == 0){
for (int i = 0; i < Dogs.size(); i++){
System.out.println(Dogs.get(i).toString());
}
}
else{
for (int i = 0; i < Dogs.size(); i++){
if(Dogs.get(i).getTailLenght() >= input){
System.out.println(Dogs.get(i).toString());
}
}
}
}
public int returnDogIndex(ArrayList<Dog> Dogs, String inputMessage, String outputMessage){
System.out.println(inputMessage);
String input = keyboard.nextLine();
for(int i = 0; i < Dogs.size(); i++){
if(input.equals(Dogs.get(i).getName())){
System.out.println(outputMessage);
return i;
}
}
return -1;
}
}
這是這最後的功能 「returnDogIndex」,我認爲是有點問題。
代碼爲狗類:
public class Dog {
private String breed, name;
private int age;
private double weight, tailLength;
public Dog(String name, String breed, int age, double weight) {
this.name = name;
this.breed = breed;
this.age = age;
this.weight = weight;
calcTailLength();
}
public void calcTailLength(){
if (breed.toLowerCase().equals("tax")) {
tailLength = 3.7;
}
else {
tailLength = (age*weight)/10;
}
}
public String toString() {
return name + " är en " + age + " år gammal " + breed + " som väger " + weight + " kg och har en svanslängd på " + tailLength;
}
public String getName() {
return name;
}
public void increaseAge(){
age++;
calcTailLength();
}
public double getTailLenght(){
return tailLength;
}
}
對不起壞的標題和這樣 – Olof
這情況下,你要什麼都不用做? 「列表」和「刪除」? –
我確實需要它們,因爲它們是作業的一部分。但是在任何命令輸入之後,「刪除」和「增加年齡」不起作用,這是前面提到的。 – Olof