带有四个数字的Java填充数组到循环生成的数组中

时间:2016-04-15 10:14:00

标签: java dynamic-arrays

我仍然习惯于Java,所以这是我的问题首先:

我正在制作一个乐透计划,我的想法是程序会询问有多少玩家在玩。

接下来是他们的名字和他们选择的4个号码。

我有一个阵列的ArrayList,我想将它们的四个数字存储到单独的数组中,我希望数组能够与玩家的数量成比例地动态增长。

E.g:

如果正在玩3个玩家,则会制作3个阵列。

如果有1,000,000名玩家在玩,将会制作1,000,000个阵列。

[我的代码也跳过4次迭代,要求4个数字。

我的代码:

import java.util.ArrayList;
import java.util.Scanner;

public class lotto_test {
public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter Amount of Players Playing: ");
    int players = input.nextInt();

    String[] playerNames = new String[players];

    int[] lottoNumbers = new int[4];
    lottoNumbers[0] = 1;
    lottoNumbers[1] = 2;
    lottoNumbers[2] = 3;
    lottoNumbers[3] = 4;

    ArrayList<int[]> entryNumbers = new ArrayList<>();

    int i;
    int j;
    int k;

    for(k = 0; k < players; k++){

        entryNumbers.add(new int[4]);

    }

    for(i = 0; i < players; i++)
    {

        Scanner loopInput = new Scanner(System.in);
        System.out.println("Please Enter Your Name Player " + (i+1) + " :");
        playerNames[i] = loopInput.next();

        for(j = i; j <= i; j++){

            System.out.println("Player " + (j + 1) + " : Enter Number " + (j + 1) + " :");

            entryNumbers.get(i)[j] = loopInput.nextInt();

        }//End For Loop (j)

    }//End For Loop (i)

    for(int p = 0; p < players; p++){

        System.out.println("Numbers : " + entryNumbers.get(p)[0] + entryNumbers.get(p)[1] + entryNumbers.get(p)[2] + entryNumbers.get(p)[3]);

    }
}
}

2 个答案:

答案 0 :(得分:0)

for(j = i; j&lt; = i; j ++)更改为for(j = 0; j <4; j ++)

for(i = 0; i < players; i++)
        {

            Scanner loopInput = new Scanner(System.in);
            System.out.println("Please Enter Your Name Player " + (i+1) + " :");
            playerNames[i] = loopInput.next();

            for(j = 0; j < 4; j++){

                System.out.println("Player " + (i + 1) + " : Enter Number " + (j + 1) + " :");

                entryNumbers.get(i)[j] = loopInput.nextInt();

            }//End For Loop (j)

        }

答案 1 :(得分:0)

请参阅以下代码以了解您的知识。

import java.util.ArrayList;
import java.util.Scanner;

public class LottoTest {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter Amount of Players Playing: ");
        int noOfPlayers = input.nextInt();

        ArrayList<Player> players = new ArrayList<Player>();

        Player player;
        // Scanner loopInput = new Scanner(System.in);
        for (int i = 0; i < noOfPlayers; i++) {

            System.out.println("Please Enter Name of Player " + (i + 1) + " :");

            player = new Player(input.next());

            for (int numbers = 0; numbers < 4; numbers++) {

                System.out.println("Player " + player.name + " : Enter Number " + (numbers + 1) + " :");

                player.setNumber(input.nextInt(), numbers);

            }// End For Loop (j)

            players.add(player);

        }// End For Loop (i)
        input.close();
        for (Player p : players) {

            System.out.println("Player:" + p.name + " Numbers : " + p.getNumber(0) + ":" + p.getNumber(1) + ":"
                    + p.getNumber(2) + ":" + p.getNumber(3));

        }
    }
}

class Player {
    String name;
    private int[] lottoNumbers = new int[4];

    public Player(String name) {
        this.name = name;
    }

    public void setNumber(int number, int index) {
        if (index < 4)
            lottoNumbers[index] = number;
    }

    public int getNumber(int index) {
        if (index < 4)
            return lottoNumbers[index];
        return 0;
    }

}