Java 垂直显示而不是水平显示的数组
我对编程有点陌生,我正在做一个关于数组的学校作业 我想写一个程序,使用数组存储统计信息Java 垂直显示而不是水平显示的数组,java,arrays,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(); stat
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;
}
}
import java.io.*;
公共级曲棍球联赛{
静态最终整数行=7;
静态最终整数Cols=8;
静态双曲棍球图[][]=新双[行][Cols];
静态曲棍球联赛HL=新曲棍球联赛();
静态BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
公共静态void main(字符串args[])引发IOException{
while(true){
System.out.println(“欢迎来到NHL统计程序”);
System.out.println(“您想继续该程序吗?”);
最终字符串menuDecision=br.readLine();
if(menuDecision.equals(“n”)){
System.out.println(“再见”);
系统出口(0);
}
if(menuDecision.equals(“y”)){
while(true){
这8支队伍分别是多伦多枫叶队、蒙特利尔加拿大人队、渥太华参议员队、底特律红翼队、波士顿棕熊队+
“芝加哥黑鹰、纽约岛民和匹兹堡企鹅”);
System.out.println(“要输入多伦多的统计数据,请输入“0”);
System.out.println(“要输入蒙特利尔的统计数据,请输入'1'”);
System.out.println(“要输入渥太华的统计数据,请输入'2'”);
System.out.println(“要输入底特律的统计数据,请输入'3'”);
System.out.println(“要输入波士顿的统计数据,请输入'4'”);
System.out.println(“要输入芝加哥的统计数据,请输入'5'”);
System.out.println(“要输入纽约的统计数据,请输入'6'”);
System.out.println(“要输入匹兹堡的统计数据,请输入'7'”);
int numString=Integer.parseInt(br.readLine());
信息(numString);
}
}
}
}
公共静态双[][]信息(int teamInput)引发IOException{
System.out.println(“输入所玩游戏的数量”);
int games=Integer.parseInt(br.readLine());
曲棍球图[0+团队输入][1]=游戏;
System.out.println(“输入赢款金额”);
int=Integer.parseInt(br.readLine());
曲棍球图[0+teamInput][2]=获胜;
System.out.println(“输入领带数量”);
int ties=Integer.parseInt(br.readLine());
曲棍球图[0+teamInput][3]=平局;
System.out.println(“输入损失金额”);
int loss=Integer.parseInt(br.readLine());
曲棍球图[0+teamInput][4]=损失;
System.out.println(“输入进球数量”);
int goals=Integer.parseInt(br.readLine());
曲棍球图[0+团队输入][5]=目标;
对于(int i=0;i
这就是我提出的计划。我不明白为什么我得到的输出是一个长长的垂直数字行,而不是并排的数字行
任何帮助都将不胜感激!感谢您的Info方法,在迭代数组时,您使用的是System.out.println(),而不是需要使用System.out.print()方法
for(int i=0;i
第二个println语句将帮助您在打印一行后移到下一行。查看您的循环:
for (int j = 0; j < Cols;j ++) {
System.out.println(HockeyChart[i][j] + " ");
}
您使用的是
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;
for(int i=0;i
除此之外,我强烈建议您学习Java命名约定并开始遵循它们——变量和方法应该以小写字母开头,而您的Info
方法可能更像enterTeamInfo
。我强烈建议大家也要养成把所有领域都保密的习惯。
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}
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;