Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组/二维数组用户输入和显示_Java_Arrays - Fatal编程技术网

Java数组/二维数组用户输入和显示

Java数组/二维数组用户输入和显示,java,arrays,Java,Arrays,我正在尝试编写一个方法,允许用户输入运动队中的球员人数、比赛次数、球员姓名以及每个球员每场比赛的得分 基本上,我需要以下显示输出,其中所有信息均由用户提供: Player Game 1 Game 2 Game 3 Game 4 Tom Dick Harry Matthew John 。。。然后是每场比赛中每个球员的得分 我承认这个小项目源于家庭作业,但我已经交了作业,作业甚至没有要求用户提供输入。我真的只是想知道怎么做 下面是我的代码。有几个问题 我将每个玩家的名字分配给一个名为pla

我正在尝试编写一个方法,允许用户输入运动队中的球员人数、比赛次数、球员姓名以及每个球员每场比赛的得分

基本上,我需要以下显示输出,其中所有信息均由用户提供:

Player  Game 1  Game 2  Game 3  Game 4
Tom
Dick
Harry
Matthew
John
。。。然后是每场比赛中每个球员的得分

我承认这个小项目源于家庭作业,但我已经交了作业,作业甚至没有要求用户提供输入。我真的只是想知道怎么做

下面是我的代码。有几个问题

  • 我将每个玩家的名字分配给一个名为
    playerRoster
    的数组。不是所有数组都在
    arrayName[0]
    处初始化吗?但是当我打印出
    playerRoster[0]
    时,什么也不显示。当我打印出
    playerRoster[1]
    时,将显示我为数组输入的第一个名称(该名称应在
    playerRoster[0]
    处建立索引)。为什么?

  • 与上面相关:我被提示输入花名册上的球员人数,并说我输入了数字5。但是提示只允许我输入4个名称。为什么?

  • 上面的显示就是我想要的,我确信有内置的Java方法来显示值表。我其实不想那样。我有点想继续循环,就像我现在做的那样。(我知道,我知道——乞丐不能挑肥拣瘦)

  • 这是我的密码

    import java.util.*;
    
    public class BasketballScores
    {
      public static void main (String[] args)
      {
        Scanner input = new Scanner(System.in);    
        System.out.println ("Enter the number of players on roster: ");
        int numPlayers = input.nextInt();
        System.out.println();
        System.out.println ("Enter the number of games played: ");
        int numGames = input.nextInt();
        System.out.println();
        int[][] arrayScores = new int[numPlayers][numGames];
        String[] playerRoster = new String[numPlayers];
        System.out.println ("Enter player names: ");
        for (int j = 0; j < numPlayers; j++)
        {
            playerRoster[j] = input.nextLine();
        }
        for (String element: playerRoster)
        {
            System.out.println (element);
        }
        System.out.println();
        System.out.println ("Enter the points scored per player per game: ");
        System.out.println();
    
        System.out.print (playerRoster[1]);
        for (int i = 0; i < numPlayers; i++)
        {
            for (int k = 0; k < numGames; k++)
            {
                arrayScores[i][k] = input.nextInt();
            }
        }
        System.out.println();
    
        for (int i = 0; i < numPlayers; i++)
        {
            System.out.println();
            for (int k = 0; k < numGames; k++)
            {
                System.out.print (arrayScores[i][k] + ", ");
            }
        }
      }
    }
    
    import java.util.*;
    公开课篮球成绩
    {
    公共静态void main(字符串[]args)
    {
    扫描仪输入=新扫描仪(System.in);
    System.out.println(“输入花名册上的球员人数:”);
    int numPlayers=input.nextInt();
    System.out.println();
    System.out.println(“输入玩的游戏数:”);
    int numGames=input.nextInt();
    System.out.println();
    int[]arrayScores=新的int[numPlayers][numGames];
    String[]playerRoster=新字符串[numPlayers];
    System.out.println(“输入玩家名称:”);
    对于(int j=0;j
    首先,在您获得玩家姓名之前,您仍在使用
    numGames
    。在尝试解析名称之前,需要调用
    input.nextLine()
    。这将解决您的前两个问题。使用
    #nextLine
    时,您将到达当前行的最后一行。在你拿到你的
    numGames
    后,你仍然在那条线上。调用
    input.nextLine()

    System.out.println ("Enter the number of players on roster: ");
    int numPlayers = input.nextInt();
    System.out.println();
    System.out.println ("Enter the number of games played: ");
    int numGames = input.nextInt();
    input.nextLine(); //You need this to go to the next line before attempting to get your player names. 
    System.out.println();
    int[][] arrayScores = new int[numPlayers][numGames];
    String[] playerRoster = new String[numPlayers];
    System.out.println ("Enter player names: ");
    for (int j = 0; j < numPlayers; j++)
    {
        playerRoster[j] = input.nextLine();
    }
    
    输出:

    Player    Game1 Game2 Game3 Game4 
    Tom         2     9     7     3
    Dick        8     1     8     4
    Harry       9     2     0     2
    Matthew     5     1     3     2
    Johh        8     4     0     2
    

    一旦你分享了你是如何输入数据的,我可以帮你更多的忙。希望这有帮助

    你能给我们看看你的意见吗?Gonzo,非常感谢你的帮助。由于您的建议,我能够修复名称的显示方式,这是一个很大的帮助。您问我打算如何输入数据。也许我没有正确理解这个问题,但这不是我的数据输入吗?:System.out.println(“输入花名册上的球员人数:”);int numPlayers=input.nextInt();System.out.println();System.out.println(“输入玩的游戏数:”);int numGames=input.nextInt();System.out.println();int[]arrayScores=新的int[numPlayers][numGames];String[]playerRoster=新字符串[numPlayers];System.out.println(“输入玩家名称:”);对于(intj=0;jPlayer Game1 Game2 Game3 Game4 Tom 2 9 7 3 Dick 8 1 8 4 Harry 9 2 0 2 Matthew 5 1 3 2 Johh 8 4 0 2