Java使用Scanner构建对象数组编译成功无输出求助
嘿,我帮你揪出代码里的几个关键问题啦,咱们一步步拆解:
核心问题:程序入口根本没执行实际逻辑
你在外部类Sanctuary的main方法里只定义了内部类Animal,但完全没有调用这个内部类的任何逻辑。Java程序只会执行外部类的入口main方法,你这里的入口方法除了定义类啥也没做,所以编译通过后直接结束,输出面板自然是空的。
第二个坑:Scanner输入的换行符残留陷阱
当你用sc.nextDouble()读取数字后,输入流里会留下一个换行符\n,接下来的sc.nextLine()会直接读取这个空的换行符,导致后续的字符串输入(比如gender、age)要么读空值,要么因为Integer.parseInt解析空字符串抛出异常,直接打断程序。
修复后的完整代码
我把问题都修正了,你可以直接参考:
package sanctuary; import java.util.Scanner; public class Sanctuary { /** * @param args the command line arguments */ public static void main(String[] args) { // 把逻辑直接放在外部类的入口main里,不用在内部类嵌套main Animal[] animalList = new Animal[2]; // 先设为2个方便测试,之后可以改回100 Scanner sc = new Scanner(System.in); // 只创建一个Scanner,避免重复初始化 // 循环创建动物对象并录入数据 for(int i=0;i<animalList.length;i++){ animalList[i] = new Animal(); System.out.print("\nEnter details of "+ (i+1) +" Animal\n"); animalList[i].getAnimalData(sc); } // 输出所有动物信息 System.out.print("\nAnimal Details\n"); for(int i=0;i<animalList.length;i++){ animalList[i].printAnimalData(); } sc.close(); // 记得关闭Scanner释放资源 } // 把Animal改成静态内部类,不然外部类main无法直接实例化它 static class Animal { private String species; private String animalname; private String breed; private double weight; private String gender; private int age; private String location; private String vet; private double vaccine; private double medicine; private double food; private double muandv; // 传入统一的Scanner,同时处理换行符问题 void getAnimalData(Scanner sc) { System.out.print("\n\tEnter Animal Species : "); species = sc.nextLine(); System.out.print("\n\tEnter Animal Name : "); animalname = sc.nextLine(); System.out.print("\n\tEnter Animal Breed : "); breed = sc.nextLine(); System.out.print("\n\tEnter Animal Weight : "); weight = sc.nextDouble(); sc.nextLine(); // 关键:吃掉nextDouble后残留的换行符 System.out.print("\n\tEnter Animal Gender : "); gender = sc.nextLine(); System.out.print("\n\tEnter Animal Age : "); age = Integer.parseInt(sc.nextLine()); System.out.print("\n\tEnter Animal Location : "); location = sc.nextLine(); System.out.print("\n\tEnter Vet Name: "); vet = sc.nextLine(); System.out.print("\n\tEnter Vaccine Cost : "); vaccine = sc.nextDouble(); sc.nextLine(); // 同样处理换行符 System.out.print("\n\tEnter Medicine Cost : "); medicine = sc.nextDouble(); sc.nextLine(); System.out.print("\n\tEnter Food Cost : "); food = sc.nextDouble(); sc.nextLine(); System.out.print("\n\tEnter Maintenance, Utility and Vet Cost : "); muandv = sc.nextDouble(); sc.nextLine(); } void printAnimalData() { System.out.print("\n\t" + species + "\t" +animalname + "\t" +breed + "\t" +weight + "\t" +gender + "\t" +age + "\t" +location + "\t" +vet + "\t" +vaccine + "\t" +medicine + "\t" +food + "\t" +muandv); } } }
关键调整说明
- 移除内部类的
main方法:把业务逻辑移到外部类的入口main里,这才是程序真正会执行的入口。 - 将
Animal改为静态内部类:非静态内部类必须依赖外部类实例才能创建,静态内部类可以直接在外部类main里实例化。 - 统一管理Scanner+处理换行符:只创建一个Scanner,避免资源浪费;每次用
nextDouble()后调用sc.nextLine()吃掉残留的换行符,解决输入错位问题。 - 缩小数组长度测试:先设为2个动物,方便快速验证,没问题后再改回100。
测试一下,现在应该能正常输入所有数据,最后看到完整的动物信息输出啦!
内容的提问来源于stack exchange,提问作者ClementB
相关产品推荐
相关产品推荐

