数组垂直而不是水平显示

时间:2014-12-08 06:50:59

标签: java arrays

我对编程很陌生,我正在研究数组上的学校作业

我想编写一个使用数组存储统计信息的程序。

import java.io.*;
public class HockeyLeague {
static final int Rows = 7;
static final int Cols = 8;
static double HockeyChart [][] = new double[Rows][Cols];
static HockeyLeague HL = new HockeyLeague();
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws IOException {
    while(true){
        System.out.println("Welcome to the NHL statistic program");
        System.out.println("Would you like to proceed to the program? <y for yes, n for no>");
        final String menuDecision = br.readLine();
        if (menuDecision.equals("n")) {
            System.out.println("Goodbye");
            System.exit(0);
        }
        if (menuDecision.equals("y")) {
            while(true) {
            System.out.println("The 8 teams are Toronto Maple Leafs, Montreal Canadiens, Ottawa Senators, Detroit Red Wings, Boston Bruins,"+
            " Chicago Blackhawks, New York Islanders, and Pitsburg Penguins");
            System.out.println("To input statistics for Toronto, enter '0' ");
            System.out.println("To input statistics for Montreal, enter '1' ");
            System.out.println("To input statistics for Ottawa, enter '2' ");
            System.out.println("To input statistics for Detroit, enter '3' ");
            System.out.println("To input statistics for Boston, enter '4' ");
            System.out.println("To input statistics for Chicago, enter '5' ");
            System.out.println("To input statistics for New York, enter '6' ");
            System.out.println("To input statistics for Pitsburg, enter '7' ");
            int numString = Integer.parseInt(br.readLine());

            Info (numString);

           }
       }
    }       
}

public static double[][] Info(int teamInput)throws IOException{
    System.out.println("Enter the amount of games played");
    int games = Integer.parseInt(br.readLine());
    HockeyChart [0+teamInput][1] = games;
    System.out.println("Enter the amount of wins");
    int wins = Integer.parseInt(br.readLine());
    HockeyChart [0+teamInput][2] = wins;
    System.out.println("Enter the amount of ties");
    int ties = Integer.parseInt(br.readLine());
    HockeyChart [0+teamInput][3] = ties;
    System.out.println("Enter the amount of losses");
    int losses = Integer.parseInt(br.readLine());
    HockeyChart [0+teamInput][4] = losses;
    System.out.println("Enter the amount of goals scored");
    int goals = Integer.parseInt(br.readLine());
    HockeyChart [0+teamInput][5] = goals;
    for (int i = 0; i < Rows; i ++) {
        for (int j = 0; j < Cols;j ++) {
            System.out.println(HockeyChart[i][j] + " ");
        }
        System.out.println("  ");
    }
    return HockeyChart;
 }  
}

这是我提出的计划。我不明白为什么我得到的输出是一个长的垂直行数而不是数字行并排。

任何帮助将不胜感激!感谢

3 个答案:

答案 0 :(得分:0)

在Info方法中,在迭代数组时,您使用的是System.out.println(),而不是需要使用System.out.print()方法。

for (int i = 0; i < Rows; i ++) {
    for (int j = 0; j < Cols;j ++) {
        System.out.print(HockeyChart[i][j] + " ");
    }
    System.out.println();
}

第二个println语句将帮助您在打印一行后移动到下一行。

答案 1 :(得分:0)

看看你的循环:

for (int j = 0; j < Cols;j ++) {
    System.out.println(HockeyChart[i][j] + " ");
}

println是Print Line的缩写 - 它打印给定的字符串,然后移动到下一行。如果要在一行中打印数组的所有内容,可以使用System.out.print代替:

for (int j = 0; j < Cols;j ++) {
    System.out.print(HockeyChart[i][j] + " ");
}

答案 2 :(得分:0)

您正在使用System.out.println,这意味着在新行中打印。

您可以将System.out.print用于同一行,即水平行。

代码将是这样的

for (int i = 0; i < Rows; i ++)
{
    for (int j = 0; j < Cols;j ++) {
        System.out.print(HockeyChart[i][j] + " ");
    }
    System.out.println("  ");
}
return HockeyChart;