Java - 使用 Scanner 构建对象数组

标签 java arrays java.util.scanner

我正在尝试使用用户的输入构建对象数组。我的代码运行没有错误,但输出 Pane 是空白的,并显示“构建成功”,我无法弄清楚我做错了什么,因为我确信所有代码都是正确的。任何指示将不胜感激。预先感谢您

package sanctuary;

import java.util.Scanner;

/**
 *
 * @author dell
 */
public class Sanctuary {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

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;

        void GetAnimalData()           // Defining GetAnimalData()
        {

            Scanner sc = new Scanner(System.in);

            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());

            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());

            System.out.print("\n\tEnter Medicine Cost : ");
            medicine = sc.nextDouble();

            System.out.print("\n\tEnter Food Cost : ");
            food = (sc.nextDouble());

            System.out.print("\n\tEnter Maintenance, Utility and Vet Cost : ");
            muandv = (sc.nextDouble());

        }

        void PrintAnimalData()           // Defining 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);
        }

        public void main(String args[])
        {

            Animal[] AnimalList = new Animal[100];
            int i = 0;

            for(i=0;i<AnimalList.length;i++)
                AnimalList[i] =  new Animal();   // Allocating memory to each object

            for(i=0;i<AnimalList.length;i++)
            {
                System.out.print("\nEnter details of "+ (i+1) +" Animal\n");
                AnimalList[i].GetAnimalData();
            }

            System.out.print("\nAnimal Details\n");
            for(i=0;i<AnimalList.length;i++)
                AnimalList[i].PrintAnimalData();

        }
}
    }
}

最佳答案

您的 main 方法不执行任何操作:它只包含声明。

关于Java - 使用 Scanner 构建对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112604/

相关文章:

java - 尝试从 ArrayList 中为每个用户打印出来

java - 如何使用 Scanner (Java) 只读取一个值?

java - 如何从扫描仪库读取任何用户输入?

java - 误解嵌套 for 循环时间复杂度分析的小细节......如何区分 O(n) 和 O(n²)

java - Struts 2 和一个简单的 Facebook API

java - Firestore whereEqualTo、orderBy 和 limit(1) 不起作用

C++,Integer和Char数组转换麻烦

arrays - Ruby 中哪个更快, `arr += [x]` 或 `arr << x`

java - 无法使用 Java 中的 Scanner 比较从 txt 文件读取的行

java - 如何通过代理从 HTTPS Url 获取数据?