0

我正在编写一个菜单驱动的程序,允许用户创建一定数量的团队,然后记录这些团队的胜负。我想格式化我的输出,让它看起来很干净,但是,这样做有点麻烦。如何创建赢/输列?

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"));

}

}
4

2 回答 2

0

使用System.out.format。您可以设置字段长度,如 System.out.format("%50s%50s", string1, string2);

于 2013-03-11T04:33:05.643 回答
0

您可以使用System.out.printforString.format方法。请注意,我们使用“-”标志将宽度设置为 20。请参阅说明。

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   

酷吧?:-)

于 2013-03-11T04:37:41.177 回答