Java 如何从阵列中使用的数据打印信息?
我的代码要求用户输入6支不同的运动队在一个赛季中取得的胜利、失败和平局数量。我怎样才能使它在收到所有信息后,打印出每个团队获得的胜利、平局和失利数量,并显示每个团队的总数 代码:Java 如何从阵列中使用的数据打印信息?,java,arrays,Java,Arrays,我的代码要求用户输入6支不同的运动队在一个赛季中取得的胜利、失败和平局数量。我怎样才能使它在收到所有信息后,打印出每个团队获得的胜利、平局和失利数量,并显示每个团队的总数 代码: package SMKTeamStandings; import java.util.Scanner; public class SMKTeamStandings { public static Scanner in = new Scanner(System.in); public static int num
package SMKTeamStandings;
import java.util.Scanner;
public class SMKTeamStandings {
public static Scanner in = new Scanner(System.in);
public static int number(int max, int min) {
int teamchoice = 0;
for (boolean valid = false; valid == false;) {
teamchoice = in.nextInt();
if (teamchoice >= min && teamchoice <= max) {
valid = true;
} else {
System.out.println("Please enter a different value.");
}
}
return teamchoice;
}
public static boolean finished(boolean[] completedArray) {
int i = 0;
boolean done;
for (done = true; done == true;) {
if (completedArray[i++] == false) {
done = false;
}
}
return done;
}
public static void main(String[] args) {
int teamChoice = 0, gamesNum;
String[] sportteams = {"Basketball", "Football",
"Hockey", "Rugby",
"Soccer", "Volleyball"};
boolean[] completed = new boolean[sportteams.length];
int[][] Outcome = new int[64][sportteams.length];
for (boolean done = false; done == false;) {
for (int i = 0; i < sportteams.length; i++) {
System.out.print(i + 1 + " - " + sportteams[i]);
if (completed[i] == true) {
System.out.println(" - Finished");
} else {
System.out.println();
}
}
System.out.print("\nChoose a team from the list above:");
teamChoice = number(6, 1);
teamChoice--;
System.out.print("\nHow many games total did the " + sportteams[teamChoice]
+ " team play this season?: ");
gamesNum = in.nextInt();
System.out.format("\n %10s %10s %10s %10s %10s \n\n", "", "Possible Outcomes:",
"1 - Win",
"2 - Tie",
"3 - Loss");
for (int wintieloss = 0; wintieloss < gamesNum; wintieloss++) {
System.out.print("\nEnter the outcome for game "
+ (wintieloss + 1) + ": ");
Outcome[wintieloss][teamChoice] = number(3, 1);
}
System.out.println("\n");
completed[teamChoice] = true;
done = finished(completed);
包装SMKTeamStandings;
导入java.util.Scanner;
公共级SMKTEAMS排名{
公共静态扫描仪in=新扫描仪(System.in);
公共静态整数(整数最大值,整数最小值){
int teamchoice=0;
for(布尔值valid=false;valid=false;){
teamchoice=in.nextInt();
如果(teamchoice>=min&&teamchoice如果我理解正确,您只想输出从用户那里获得的数据。为此,您可以使用for循环遍历数据数组,并使用索引访问数据
for(int team = 0; team < sportteams.length; team++) { // for each team
System.out.println((team + 1) + " - " + sportteams[team]); // output the team
int game = 0; // index of the current game
while(Outcome[game][team] != 0) { // while there is data
System.out.print("Game " + (game + 1) ": " + Outcome[game][team] + " "); // print the data
game++; // increment the index
}
System.out.println("Total games: " + game); // print the last index == total number of games
System.out.println();
}
for(int-team=0;team
一般来说,我对Java非常陌生,所以请对我放松点,尽量不要烤我太多哈哈。我现在真的很想检查代码,但是你可以用“!valid”来替换for循环中的“valid==false;”。