import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList al = new ArrayList();
for(int i = 0;i<=10;i++){
Scanner input = new Scanner(System.in);
System.out.println("请输入猫的颜色:");
String col = input.next();
Cat c = new Cat();
c.setColor(col);
al.add();
System.out.println("请输入猫的体重:");
double wei = input.nextDouble();
c.setWeight(wei);
System.out.println("请输入猫的身高:");
double hei = input.nextDouble();
c.setWeight(hei);
System.out.println("是否继续 y/n");
char choose = input.next().charAt(0);
if(choose=='y'){
continue;
}else{
break;
}
}
System.out.println("打印信息:");
Cat c = new Cat();
for(int j =0;j<=10;j++){
System.out.println(c.getinfo());
}
}
}
我一共写了2各类,现在就是要在测试类中输入各项数据,利用ArrayList添加,然后再循环输出,这里改怎么做吗,求指导
ArrayListæ·»å 以åè¾åºï¼å¯ä»¥ä½¿ç¨getï¼positionï¼æ¥éåæ¯ä¸ªéåçæ°æ®ï¼å®ä¾å¦ä¸ï¼
import java.util.ArrayList;下面遍历输出的那个al报错了,显示Type mismatch: cannot convert from element type Object to Cat,这是什么原因吗
追答哦 不好意思,刚才忘记修改前面了
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList al = new ArrayList();
for(int i = 0;i<=10;i++){
Scanner input = new Scanner(System.in);
System.out.println("请输入猫的颜色:");
String col = input.next();
Cat c = new Cat();
c.setColor(col);
al.add(c);
System.out.println("请输入猫的体重:");
double wei = input.nextDouble();
c.setWeight(wei);
System.out.println("请输入猫的身高:");
double hei = input.nextDouble();
c.setWeight(hei);
System.out.println("是否继续 y/n");
char choose = input.next().charAt(0);
if(choose=='y'){
continue;
}else{
break;
}
}
System.out.println("打印信息:");
for(Cat c:al ){
System.out.println(c.getinfo());
}
}