简体   繁体   中英

Java array/2 Dimensional Array User Input and Display

I'm attempting to write a method that allows the user to input the number of players on a sports team, the number of games played, the names of the players, and the amount of points scored per player per game.

Basically, I want the following display output where all information has been provided by the user:

Player  Game 1  Game 2  Game 3  Game 4
Tom
Dick
Harry
Matthew
John

... and then points scored by each player in each game.

I admit that this little project originated from a homework assignment, but I've already turned the assignment in and the assignment didn't even ask for user supplied input. I really just want to know how to do this.

Below is my code. A few problems.

  1. I assign each player's name to an array called playerRoster . Don't all array initialize at arrayName[0] ? But when I print out playerRoster[0] , nothing displays. When I print out playerRoster[1] , the first name I enter for the array (which should be indexed at playerRoster[0] ) displays. Why?

  2. Related to above: I am prompted to enter the number of players on roster, and say I enter the number 5. But the prompt then allows me to only enter 4 names. Why?

  3. The display above is what I'm looking for, and I'm sure there are built in Java methods for displaying a table of values. I actually don't want that. I kind of want to go forward with loops, like I'm doing now. (I know, I know - beggars can't be choosers)

Here is my code

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] + ", ");
        }
    }
  }
}

First, before you get your player names, you are still on the line with the numGames . You need to call input.nextLine() before attempting to parse your names. This should solve your first 2 questions. What happens with #nextLine is you are going to the end of the line of the current line you are on. After you get your numGames you are still on that line. Call input.nextLine() to advance to the lines with your player names.

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();
}

For formatting your print you can use System.out.printf . Here is a quick example for your situation that you should be able to utilize. Quick reference guide

String[] players = new String[]{"Tom", "Dick", "Harry", "Matthew", "Johh"};
System.out.printf("%-10s%6s%6s%6s%6s","Player", "Game1 ", "Game2 ", "Game3 ", "Game4 ");
System.out.println();
Random r = new Random();
for (String player : players){
    System.out.printf("%-10s%3d%6d%6d%6d",player, r.nextInt(10), r.nextInt(10), r.nextInt(10), r.nextInt(10));
    System.out.println();
}

Output:

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

Once you share how you are trying to input your data, I can help a little more. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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