我正在編寫一個菜單驅動的程序,允許用戶創建一定數量的團隊,然後記錄這些團隊的勝利和損失。我想格式化我的輸出,所以看起來很乾淨,但是我這樣做有點麻煩。我如何創建一個勝/負列?在java中格式化數組值
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"));
}
}
什麼問題?也許你需要添加'\ n'來添加一箇中斷線。 – 2013-03-11 04:28:23
您已經發布了*方式*太多的代碼。大部分與你的問題無關。請創建[一個簡短的,自包含的,正確的(可編譯的)示例](http://sscce.org/)。 – ruakh 2013-03-11 04:28:51
代碼很長,但問題很簡單:-) – ktm5124 2013-03-11 04:41:29