使用Scanner将用户输入添加到Java对象数组中

问题描述 投票:0回答:1

我是一名三级学生,正在尝试为我的课程建立一个Java程序。到目前为止,我已经设法创建了一个对象数组,如果我自己编写每个对象,我可以添加它。我想添加功能,以便用户提供创建对象的信息,然后将其存储在数组中。我之前使用过扫描仪,但似乎无法找出将用户输入代码放在主类中的位置。对不起,如果这是一个非常基本的问题,但我迷路了!先感谢您。

Main Class import java.util.Scanner;

公共类Main {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Animal[] animals = new Animal[50];

    instantiateAll(animals);
    setAll(animals);
    displayAll(animals);



}
public static void instantiateAll(Animal[] animals)
{
    for (int ctr = 0; ctr < animals.length; ctr++)
        animals[ctr] = new Animal();

}
public static void setAll(Animal[] animals)
{ for (int ctr = 0; ctr < animals.length; ctr++) {

    animals[ctr].setAnimalname("Not Set Yet");
    animals[ctr].setSpecies("Not Set Yet");
    animals[ctr].setBreed("Not Set Yet");
    animals[ctr].setWeight(0);
    animals[ctr].setGender("Not Set Yet");
    animals[ctr].setAge(0);
    animals[ctr].setVet("Not Set Yet");
    animals[ctr].setVaccine(0);
    animals[ctr].setMedicine(0);
    animals[ctr].setFood(0);
    animals[ctr].setMuandv(0);
}}
public static void displayAll(Animal[] animals)
{ for (int ctr = 0; ctr < animals.length; ctr++)
    System.out.println("York Animals"+"\n"+"Animal Name:  "+animals[ctr].getAnimalname()+"\n" + "Species:   "+animals[ctr].getSpecies() + "\n" +"Breed:  "+animals[ctr].getBreed()+"\n" + "Weight:  "+animals[ctr].getWeight()+"\n" + "Gender:  "+animals[ctr].getGender()+"\n" +"Age:  "+animals[ctr].getAge()+"\n" + "Vet:  "+animals[ctr].getVet()+"\n" +"Vaccine Cost:  £"+animals[ctr].getVaccine()+"\n" +"Medicine Cost:  £"+animals[ctr].getMedicine()+"\n" +"Food Cost:  £"+animals[ctr].getFood()+"\n" + "Maintenance Cost:  £"+animals[ctr].getMuandv()+ "\n..................................................\n");}

}

动物类

public class Animal {

private String species;
private String animalname;
private String breed;
private double weight;
private String gender;
private int age;
private String vet;
private double vaccine;
private double medicine;
private double food;
private double muandv;

  public Animal(){
    this("Unknown Species","Unknown Name", "Unknown Breed", 0, "Unknown", 0, "No Vet Assigned", 0,0,0,0);
      }

    public Animal(String species,String animalname, String breed, double weight, String gender, int age, String vet, double vaccine, double medicine, double food, double muandv) {
    this.species = species;
    this.animalname = animalname;
    this.breed = breed;
    this.weight = weight;
    this.gender = gender;
    this.age = age;
    this.vet = vet;
    this.vaccine = vaccine;
    this.medicine = medicine;
    this.food = food;
    this.muandv = muandv;

}

public String getSpecies() {
    return species;
}

public void setSpecies(String species) {
    this.species = species;
}

public String getAnimalname() {
    return animalname;
}

public void setAnimalname(String name) {
    this.animalname = name;
}

public String getBreed() {
    return breed;
}

public void setBreed(String breed) {
    this.breed = breed;
}

public double getWeight() {
    return weight;
}

public void setWeight(double weight) {
    this.weight = weight;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    String validGender = gender.toLowerCase(); //this validates the entry into the Gender field
    if(validGender.equals("f")|| validGender.equals("m")) {
        this.gender = gender;
    } else {
        this.gender = "Invalid Gender";

    }
        }

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getVet() {
    return vet;
}

public void setVet(String vet) {
    this.vet = vet;
}

public double getVaccine() {
    return vaccine;
}

public void setVaccine(double vaccine) {
    this.vaccine = vaccine;
}

public double getMedicine() {
    return medicine;
}

public void setMedicine(double medicine) {
    this.medicine = medicine;
}

public double getFood() {
    return food;
}

public void setFood(double food) {
    this.food = food;
}

public double getMuandv() {
    return muandv;
}

public void setMuandv(double muandv) {
    this.muandv = muandv;
}
java arrays java.util.scanner
1个回答
0
投票

你可以做点什么

public static void main(String... args){
        Scanner input = new Scanner(System.in);
        Animal[] animals = new Animal[50];
        for(int i=0;i<animals.length;i++)
            animals[i] = getAnimal(input);
//        instantiateAll(animals);
//        setAll(animals);
        displayAll(animals);
    }
    public static Animal getAnimal(Scanner input){
        Animal animal = new Animal();
        System.out.println("Animal Name: ");
        animal.setAnimalname(input.next());
        System.out.println("Species: ");
        animal.setSpecies(input.next());
        /*
        .
        .
        .
        * */
        return animal;
    }
© www.soinside.com 2019 - 2024. All rights reserved.