简体   繁体   中英

formatting array values in java

I'm writing a menu powered program that allows the user to create a certain number of teams, then record wins and losses of those teams. I want to format my output so it looks clean, however, I'm having a bit of trouble doing so. How can I create a wins/losses column?

import java.util.Scanner;

public class sports {


public static void main(String[] args) {


    System.out.println("Howdy sports fan!");

    String menuSelect;
    String winSelect;
    String loseSelect;
    int teamSize = 0;

    String[] teamsArray = new String[0];
    int[] winsArray = new int[0];
    int[] lossesArray = new int[0];


    do {

        System.out.println("Please pick an option from the list below:");
        System.out.println("1) Create League");
        System.out.println("2) List all teams");
        System.out.println("3) Record a win");          
        System.out.println("4) Record a loss");         
        System.out.println("5) Quit");          
        Scanner keyboard = new Scanner(System.in);
        menuSelect = keyboard.nextLine();

        if ( menuSelect.equals("1") )
        {

            System.out.println("How many teams should I make?");
            teamSize = Integer.parseInt(keyboard.nextLine());
            teamsArray = new String[teamSize];

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                System.out.println("Team " + (i+1) + "'s name?");
                teamsArray[i] = keyboard.nextLine();                
            }
        }

        else if ( menuSelect.equals("2") )
        {

            System.out.printf( "%15s %16s %n", "W", "L");

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                System.out.println(teamsArray[i]);
                System.out.printf("%15d", winsArray[i]);
                System.out.printf("%16d", lossesArray[i]);

            }
        }

        else if ( menuSelect.equals("3") )
        {
            winsArray = new int[teamSize];
            System.out.println("Which team won a game?");
            winSelect = keyboard.nextLine();

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                if ( winSelect.equals(teamsArray[i]) )
                {
                    winsArray[i]++;
                }
            }
        }

        else if ( menuSelect.equals("4") )
        {
            lossesArray = new int[teamSize];
            System.out.println("Which team lost a game?");
            loseSelect = keyboard.nextLine();

            for ( int i = 0; i < teamsArray.length; ++i )
            {
                if ( loseSelect.equals(teamsArray[i]) )
                {
                    lossesArray[i]++;
                }
            }
        }



    } while(!menuSelect.equals("5"));

}

}

Use System.out.format . You can set the field length like System.out.format("%50s%50s", string1, string2);

You can use the System.out.printf or String.format methods. Note that we are using the "-" flag to set a width of 20. See description.

public class Example {
  public static void main(String[] args) {
    String[] teams = {"Bobcats", "Tigers", "Lions", "Cheetahs", "Jackals", "Leopards", "Snow Leopards", "Cougars", "Mountain Lions", "Bobcats"};
    System.out.printf("%-30s %-20s %-20s %n", "Team Name", "No. of Wins", "No. of Losses");
    for (int i = 0; i < 10; i++) {
      System.out.printf("%-30s %-20d %-20d %n", teams[i], i, i);
    }
  }
}

$ javac Example.java
$ java Example

Team Name                      No. of Wins          No. of Losses        
Bobcats                        0                    0                    
Tigers                         1                    1                    
Lions                          2                    2                    
Cheetahs                       3                    3                    
Jackals                        4                    4                    
Leopards                       5                    5                    
Snow Leopards                  6                    6                    
Cougars                        7                    7                    
Mountain Lions                 8                    8                    
Bobcats                        9                    9   

Cool, huh? :-)

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