我有3只狗的Arraylist
,我可以在列表中注册更多的狗,通过调用toString
函数列出它们,将狗的年龄提高一个,然后移除一只狗从列表中。只要您只使用最后两个,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;
}
}
这是最后一个功能&#34; returnDogIndex&#34;我觉得这有点不对劲。
Dog类的代码:
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;
}
}
答案 0 :(得分:1)
当您找不到狗时,您将返回-1作为索引。这是第一个错误。 Java中的Array或ArrayList索引从0开始,因此返回-1将为您提供ArrayIndexOutOfBoundsException异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
at java.util.ArrayList.elementData(ArrayList.java:382)
at java.util.ArrayList.remove(ArrayList.java:459)
at com.daimler.iqm.service.DogExec.main(DogExec.java:30)
除此之外,您一直在做的最大的错误是获取特定tye的输入
keyboard.nextDouble(), keyboard.nextInt()
我建议您通过这两个StackOverflow线程来理解并解决您的问题
Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
Java String Scanner input does not wait for info
那说你需要纠正你的2方法注册并列出并在每次调用nextInt或nextDouble后添加keyboard.nextLine()
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();keyboard.nextLine();
System.out.println("Vikt:");
double weight = keyboard.nextDouble();keyboard.nextLine();
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();keyboard.nextLine();
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());
}
}
}
}