我有一个作业(Java使用Netbeans输入俱乐部名称,输入分数,然后打印表格式联赛排名)。每一行都显示我们输入的结果,并显示应该的排名(最大点的俱乐部名称位于表的顶端:我的意思是:(粗体中的用户输入)
输入编号俱乐部名称:4
输入俱乐部1:ManUtd
投入俱乐部2:利物浦
输入俱乐部3:切尔西
投入俱乐部4:阿森纳
输入分数:
ManUtd x利物浦:1 1
ManUtd x切尔西:10 0
ManUtd x阿森纳:10 0
利物浦x切尔西:1 1
利物浦x阿森纳:1 0
切尔西x阿森纳:1 1
并显示如下团队记录的输出:
球队赢了平局
曼努特3 2 1 0 7
利物浦3 1 2 0 5
切尔西3 0 2 1 2
阿森纳3 0 1 2 1
++++++++++++++++++++++++++++++++++++++++++++++
我试着把我从课本上读到的全部知识联系起来,但效果并不好。但是,不知道如何使用数组、循环函数、输入.I中的整数/数据,无法真正意识到可以引导我找到解决方案的提示。有人能帮我吗?我刚刚自学了java,新冠肺炎让我的讲师很难认识我的学习。
对于任何回复此信息的人,感谢您的帮助:)
我的基本代码卡在这里:
package standings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LeagueStandings {
public static void main(String[] args) {
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int data = 0;
String[] nama = new String[30];
System.out.println("CLUB NAME");
System.out.println("+===============INPUTAN============================+");
try{
System.out.println("How Many Clubs ? = ");
data = Integer.parseInt(input.readLine());
for (int a=1;a<=data;a++){
System.out.println("------Club No."+ a +"------");
System.out.println("Enter club name = ");
nama[a] = input.readLine();
}
} catch (IOException e ){
System.out.println("Error");
}
}预期的结果是这样的(讲师的预期结果)

发布于 2020-04-12 21:23:30
这是解决你的问题的方法。我试着编写尽可能幼稚的解决方案,因为您是初学者。
package com.standings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeagueStandings {
public static void main(String[] args) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int data = 0;
System.out.print("Input number of clubs:\t");
data = Integer.parseInt(input.readLine());
String[] teamList = new String[data];
for (int i = 0; i < data; i++) {
System.out.print("Enter club " + (i + 1) + " :\t");
teamList[i] = input.readLine();
}
System.out.println("-------------------------------------------------");
// representing 2D array of team score board where each scoreBoard is of type
// 0 1 2 3 4
// [played] [win] [draw] [loose] [points]
int[][] scoreBoard = new int[data][5];
for (int i = 0; i < data - 1; i++) {
for (int j = i + 1; j < data; j++) {
System.out.print(teamList[i] + " X " + teamList[j] + " : ");
String score = input.readLine();
List<String> scoreList = new ArrayList<String>(Arrays.asList(score.split(" ")));
scoreList.removeIf(s -> s.equals(""));
int teamAScore = Integer.parseInt(scoreList.get(0));
int teamBScore = Integer.parseInt(scoreList.get(1));
scoreBoard[i][0]++;
scoreBoard[j][0]++;
if (teamAScore > teamBScore) {
scoreBoard[i][1]++;
scoreBoard[j][3]++;
scoreBoard[i][4] += 3;
} else if (teamAScore == teamBScore) {
scoreBoard[i][2]++;
scoreBoard[j][2]++;
scoreBoard[i][4]++;
scoreBoard[j][4]++;
} else {
scoreBoard[i][3]++;
scoreBoard[j][1]++;
scoreBoard[j][4] += 3;
}
}
}
System.out.format("%15s%15s%15s%15s%15s%15s\n", "TEAMS", "PLAYED", "WIN", "DRAW", "LOOSE", "POINTS");
for (int i = 0; i < data; i++) {
System.out.format("%15s", teamList[i]);
System.out.format("%15d%15d%15d%15d%15d\n", scoreBoard[i][0], scoreBoard[i][1], scoreBoard[i][2], scoreBoard[i][3], scoreBoard[i][4]);
}
} catch (IOException e) {
System.out.println("Error");
}
}
}https://stackoverflow.com/questions/61115674
复制相似问题