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