我的代码如下,我的任务是Java多维数组,如何获得打印列数量?
正如你可以在我的代码中看到的,我有数组设置为[playerCount] [5],我认为这是造成问题,但林不确定如何解决它。有时可能会有5列,有时可能只有2,3,4。也有多行,可能是1,2,3,无穷大。我似乎无法弄清楚如何获得列的总行数,所以后来我可以在这些行中添加值来获得总和。
import java.util.Scanner;
class ASgn8
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("How many players? ");
int playerCount = scan.nextInt();
scan.nextLine();
String[] playerNames = new String[playerCount];
Die[][] diceArray = new Die[playerCount][5];
for(int i = 0; i < playerCount; i++)
{
System.out.print("What is your name: ");
playerNames[i] = scan.nextLine();
}
int randomNum = (int)(Math.random() * (30-10)) +10;
System.out.println(randomNum);
int userChoice = 1;
for(int i = 0; i < playerCount; i++)
{
int j=0;
System.out.println(playerNames[i] + "'s turn!");
while(userChoice != 2 && j < 5)
{
Die roll2 = new Die();
roll2.roll();
System.out.println(roll2);
diceArray = new Die[i][j];
System.out.println("Again? 1= yes, 2=no");
userChoice = scan.nextInt();
j++;
}
if(userChoice == 2)
{
userChoice = 1;
}
}
}
}
它是有道理的,但是我的数组被设置为[5]并且它结束了,只是我不知道如何将它声明为用户想要滚动的数量,直到循环才知道它的数量你知道有什么办法解决这个问题吗? –