简体   繁体   English

通过用户输入设置Java数组长度

[英]setting java array length via user input

How would I initialize an Array within an object who's length is that of a user's input? 如何在长度为用户输入长度的对象中初始化数组? I want to set the number of bats via user input, and make that the array length, then within the array of basesAchieved , I want to set a number based on user input (1-4) representing the base achieved. 我想通过用户输入来设置蝙蝠的数量,并使数组的长度,然后在basesAchieved数组内,我想根据用户输入(1-4)来设置一个数字,该数字代表所实现的基础。

// set up a Batter
public class Batter
{
    private String batterName;
    private int numberOfBats;
    private int[] basesAchieved;

    // fill fields with empty data, how is this done with an array??
    public Batter()
    {
        this("", 0,0);
    }

    //
    public Batter(String batterName, int numberOfBats, int[] basesAchieved)
    {
        this.batterName = batterName;
        this.numberOfBats = numberOfBats;
        this.basesAchieved = basesAchieved;
    }

    public void setBatterName(String batterName)
    {
        this.batterName = batterName;
    }

    public String getBatterName()
    {
        return batterName;
    }

    public void setNumberOfBats(int numberOfBats)
    {
        this.numberOfBats = numberOfBats;
    }

    public int getNumberOfBats()
    {
        return numberOfBats;
    }

    // want to set an array to get a number (1-4) for each number of @ bats     
    // (numberOfBats). 
    public void setBasesAchieved(int[] basesAchieved)
    {
        this.basesAchieved = ;
    }

    public int getBasesAchieved()
    {
        return basesAchieved;
    }
}

In the setter method you should assign this.basesAchieved = basesAchieved; setter方法中,您应该分配this.basesAchieved = basesAchieved; . If you want to initialize the basesAchieved with a length then just: int[] basesAchieved = new int[yourlength] in the class you initialize Batter class, then call the setter method of this class 如果要使用长度初始化basesAchieved,则只需:在初始化Batter类的类中, int[] basesAchieved = new int[yourlength] ,然后调用该类的setter方法

You have some errors in your class where you try to use an int array. 您在尝试使用int数组的类中遇到一些错误。

public Batter()
{
    this("", 0, new int[0]);
}

// skipped...

public void setBasesAchieved(int[] basesAchieved)
{
    this.basesAchieved = basesAchieved;
}

public int[] getBasesAchieved()
{
    return basesAchieved;
}

This question explains how to get user input How can I get the user input in Java? 该问题说明了如何获取用户输入如何获取Java中的用户输入?

One of the simplest ways is to use a Scanner object as follows: 最简单的方法之一是使用Scanner对象,如下所示:

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int.

You can use this method to read the number of numberOfBats and create your object with the right array length. 您可以使用此方法读取numberOfBats的数量,并使用正确的数组长度创建对象。 Then you can keep asking the user for input and put those into the basesAchieved array. 然后,您可以继续询问用户输入内容,并将其放入basesAchieved数组中。 Or you can ask the reader for all inputs first and then create your object. 或者,您可以先要求读者提供所有输入,然后创建您的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM