我将如何使用并行数组在 java 中存储不同类型的信息
How would I approach Parallel Arrays to store different types of information in java
我有以下任务:
最多可以有十个团队。 Parallel Arrays 用于存储团队名称,以及跟踪获胜次数、加时赛失败次数和得分。输入最后一个团队的结果后,程序会按照输入顺序相反的顺序输出每个团队记录的摘要。
注:"W"为2分,"L"为0分,"O"为1分
示例输入:
3 //Option number
Toronto //phrase
W //letters that loop in a while loop
W
L
O
W
O
W
N //To close the while loop, "N" is entered
Montreal
L
L
O
L
L
W
L
L
N // To close the while loop, "N" is entered
Vancouver
W
W
O
O
L
N //To close the while loop, "N" is entered
Winnipeg
L
L
L
W
O
O
W
W
W
W
W
W
W
Q //To close the while loop and get all the results, "Q" is entered
示例输出(以相反的顺序输出结果):
Team W O L P //States that first, Team and then Wins, Overtime, Losses, and then points. "W" is 2 points, "O" is 1 point, and "L" is 0 point
Winnipeg 8 2 3 18
Vancouver 2 2 1 6
Montreal 1 1 6 3
Toronto 4 2 1 10
我用选项 2 完成了类似的任务,不需要使用数组,现在这是选项 3,它只需要使用并行数组
选项 2 代码:
else if (option == 2){
int pointsW = 0;
int pointsL = 0;
int pointsO = 0;
int counter = 0;
int totalpoints = 0;
String phrase = keyboard.next();
while(go){
String letter = keyboard.next();
if (letter.equals("W")){
pointsW+=2;
}
else if (letter.equals("L")){
pointsL+=0;
}
else if (letter.equals("O")){
pointsO+=1;
}
counter++;
if (letter.equals("N")){
totalpoints = pointsW + pointsL + pointsO;
counter--;
go = false;
}
}
int counter2 = 0;
int totalpoints2 = 0;
int pointsW2 = 0;
int pointsL2 = 0;
int pointsO2 = 0;
String phrase2 = keyboard.next();
while (go2){
String letter = keyboard.next();
if (letter.equals("W")){
pointsW2+=2;
}
else if (letter.equals("L")){
pointsL2+=0;
}
else if (letter.equals("O")){
pointsO2+=1;
}
counter2++;
if (letter.equals("Q")){
counter2--;
totalpoints2 = pointsW2 + pointsL2 + pointsO2;
go2 = false;
}
}
System.out.println(phrase + " has played "+counter+" games and has earned "+totalpoints+" points");
System.out.println(phrase2 + " has played "+counter2+" games and has earned "+totalpoints2+" points");
if (totalpoints > totalpoints2){
int wins = totalpoints - totalpoints2;
System.out.println(phrase + " is in first place by "+ wins + " points");
}else{
int wins2 = totalpoints2 - totalpoints;
System.out.println(phrase2 + " is in first place by "+ wins2 + " points");
}
}
如何将并行阵列与选项 2 的相同想法合并到选项 3 中?我刚开始学习并行数组,在网上查找相关信息时找不到任何帮助。
更新
else if (option == 3){
int teams = 10;
String phrase[] = new String[teams];
int wins[] = new int [teams];
int overtime[] = new int [teams];
int loss[] = new int [teams];
int points[] = new int [teams];
int x;
for (x = 0; x < teams; x++){
phrase[x] = keyboard.next();
while (go3){
String letter = keyboard.next();
if (letter.equals("W")){
wins[x]++;
}
else if (letter.equals("L")){
loss[x]++;
}
else if (letter.equals("O")){
overtime[x]++;
}
else if (letter.equals("N") || letter.equals("Q")){
points[x] = wins[x]*2+overtime[x];
go3 = false;
if (letter.equals("Q")){
break;
}
}
}
}
System.out.println("Team W O L P");
for (int i = x; i >= 0; i--){
System.out.println(phrase[i] + " " + wins[i] + " " + overtime[i] + " " + loss[i] + " " + points[i]);
}
}
}
}
我得到错误:
您需要数组来表示姓名、胜负、加时赛和积分。但是,当您了解更多信息时,您可以通过创建一个自定义类型来更好地管理它,比如 Game
,它将具有 name
等属性,以及一个 List
类型属性,用于记录输赢、加班等
import java.util.Scanner;
public class Standings {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter option: ");
int option = keyboard.nextInt();
// Number of teams
int teams = 10;
// Array for team names
String[] teamNames = new String[teams];
// Array to record total wins for each team
int[] wins = new int[teams];
// Array to record total overtimes for each team
int[] overtimes = new int[teams];
// Array to record total losses for each team
int[] losses = new int[teams];
// Array for team points
int[] points = new int[teams];
// Variable to be used as a counter for the number of games
int x;
// The program will exit when exit = true
boolean exit = false;
if (option == 3) {
System.out.println("[Enter N to end the game for a team or Q to terminate the program]");
System.out.println("------------------------------------------------------------------");
// Input and calculation
for (x = 0; x < teams && !exit; x++) {
String letter;
boolean go = true;
System.out.print("Enter the name of team " + (x + 1) + ": ");
teamNames[x] = keyboard.next();
while (go) {
System.out.print("Enter W/L/O for match result: ");
letter = keyboard.next();
if (letter.equals("W")) {
wins[x]++;
} else if (letter.equals("O")) {
overtimes[x]++;
} else if (letter.equals("L")) {
losses[x]++;
} else if (letter.equals("N") || letter.equals("Q")) {
points[x] = wins[x] * 2 + overtimes[x];
go = false;
if (letter.equals("Q")) {
exit = true;
}
}
}
}
// Output
System.out.println("Team W O L P");
for (int i = x - 1; i >= 0; i--) {
System.out
.println(teamNames[i] + " " + wins[i] + " " + overtimes[i] + " " + losses[i] + " " + points[i]);
}
}
}
}
样本运行:
Enter option: 3
[Enter N to end the game for a team or Q to terminate the program]
------------------------------------------------------------------
Enter the name of team 1: One
Enter W/L/O for match result: W
Enter W/L/O for match result: W
Enter W/L/O for match result: L
Enter W/L/O for match result: O
Enter W/L/O for match result: N
Enter the name of team 2: Two
Enter W/L/O for match result: L
Enter W/L/O for match result: L
Enter W/L/O for match result: O
Enter W/L/O for match result: W
Enter W/L/O for match result: Q
Team W O L P
Two 1 1 2 3
One 2 1 1 5
我有以下任务:
最多可以有十个团队。 Parallel Arrays 用于存储团队名称,以及跟踪获胜次数、加时赛失败次数和得分。输入最后一个团队的结果后,程序会按照输入顺序相反的顺序输出每个团队记录的摘要。
注:"W"为2分,"L"为0分,"O"为1分
示例输入:
3 //Option number
Toronto //phrase
W //letters that loop in a while loop
W
L
O
W
O
W
N //To close the while loop, "N" is entered
Montreal
L
L
O
L
L
W
L
L
N // To close the while loop, "N" is entered
Vancouver
W
W
O
O
L
N //To close the while loop, "N" is entered
Winnipeg
L
L
L
W
O
O
W
W
W
W
W
W
W
Q //To close the while loop and get all the results, "Q" is entered
示例输出(以相反的顺序输出结果):
Team W O L P //States that first, Team and then Wins, Overtime, Losses, and then points. "W" is 2 points, "O" is 1 point, and "L" is 0 point
Winnipeg 8 2 3 18
Vancouver 2 2 1 6
Montreal 1 1 6 3
Toronto 4 2 1 10
我用选项 2 完成了类似的任务,不需要使用数组,现在这是选项 3,它只需要使用并行数组
选项 2 代码:
else if (option == 2){
int pointsW = 0;
int pointsL = 0;
int pointsO = 0;
int counter = 0;
int totalpoints = 0;
String phrase = keyboard.next();
while(go){
String letter = keyboard.next();
if (letter.equals("W")){
pointsW+=2;
}
else if (letter.equals("L")){
pointsL+=0;
}
else if (letter.equals("O")){
pointsO+=1;
}
counter++;
if (letter.equals("N")){
totalpoints = pointsW + pointsL + pointsO;
counter--;
go = false;
}
}
int counter2 = 0;
int totalpoints2 = 0;
int pointsW2 = 0;
int pointsL2 = 0;
int pointsO2 = 0;
String phrase2 = keyboard.next();
while (go2){
String letter = keyboard.next();
if (letter.equals("W")){
pointsW2+=2;
}
else if (letter.equals("L")){
pointsL2+=0;
}
else if (letter.equals("O")){
pointsO2+=1;
}
counter2++;
if (letter.equals("Q")){
counter2--;
totalpoints2 = pointsW2 + pointsL2 + pointsO2;
go2 = false;
}
}
System.out.println(phrase + " has played "+counter+" games and has earned "+totalpoints+" points");
System.out.println(phrase2 + " has played "+counter2+" games and has earned "+totalpoints2+" points");
if (totalpoints > totalpoints2){
int wins = totalpoints - totalpoints2;
System.out.println(phrase + " is in first place by "+ wins + " points");
}else{
int wins2 = totalpoints2 - totalpoints;
System.out.println(phrase2 + " is in first place by "+ wins2 + " points");
}
}
如何将并行阵列与选项 2 的相同想法合并到选项 3 中?我刚开始学习并行数组,在网上查找相关信息时找不到任何帮助。
更新
else if (option == 3){
int teams = 10;
String phrase[] = new String[teams];
int wins[] = new int [teams];
int overtime[] = new int [teams];
int loss[] = new int [teams];
int points[] = new int [teams];
int x;
for (x = 0; x < teams; x++){
phrase[x] = keyboard.next();
while (go3){
String letter = keyboard.next();
if (letter.equals("W")){
wins[x]++;
}
else if (letter.equals("L")){
loss[x]++;
}
else if (letter.equals("O")){
overtime[x]++;
}
else if (letter.equals("N") || letter.equals("Q")){
points[x] = wins[x]*2+overtime[x];
go3 = false;
if (letter.equals("Q")){
break;
}
}
}
}
System.out.println("Team W O L P");
for (int i = x; i >= 0; i--){
System.out.println(phrase[i] + " " + wins[i] + " " + overtime[i] + " " + loss[i] + " " + points[i]);
}
}
}
}
我得到错误:
您需要数组来表示姓名、胜负、加时赛和积分。但是,当您了解更多信息时,您可以通过创建一个自定义类型来更好地管理它,比如 Game
,它将具有 name
等属性,以及一个 List
类型属性,用于记录输赢、加班等
import java.util.Scanner;
public class Standings {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter option: ");
int option = keyboard.nextInt();
// Number of teams
int teams = 10;
// Array for team names
String[] teamNames = new String[teams];
// Array to record total wins for each team
int[] wins = new int[teams];
// Array to record total overtimes for each team
int[] overtimes = new int[teams];
// Array to record total losses for each team
int[] losses = new int[teams];
// Array for team points
int[] points = new int[teams];
// Variable to be used as a counter for the number of games
int x;
// The program will exit when exit = true
boolean exit = false;
if (option == 3) {
System.out.println("[Enter N to end the game for a team or Q to terminate the program]");
System.out.println("------------------------------------------------------------------");
// Input and calculation
for (x = 0; x < teams && !exit; x++) {
String letter;
boolean go = true;
System.out.print("Enter the name of team " + (x + 1) + ": ");
teamNames[x] = keyboard.next();
while (go) {
System.out.print("Enter W/L/O for match result: ");
letter = keyboard.next();
if (letter.equals("W")) {
wins[x]++;
} else if (letter.equals("O")) {
overtimes[x]++;
} else if (letter.equals("L")) {
losses[x]++;
} else if (letter.equals("N") || letter.equals("Q")) {
points[x] = wins[x] * 2 + overtimes[x];
go = false;
if (letter.equals("Q")) {
exit = true;
}
}
}
}
// Output
System.out.println("Team W O L P");
for (int i = x - 1; i >= 0; i--) {
System.out
.println(teamNames[i] + " " + wins[i] + " " + overtimes[i] + " " + losses[i] + " " + points[i]);
}
}
}
}
样本运行:
Enter option: 3
[Enter N to end the game for a team or Q to terminate the program]
------------------------------------------------------------------
Enter the name of team 1: One
Enter W/L/O for match result: W
Enter W/L/O for match result: W
Enter W/L/O for match result: L
Enter W/L/O for match result: O
Enter W/L/O for match result: N
Enter the name of team 2: Two
Enter W/L/O for match result: L
Enter W/L/O for match result: L
Enter W/L/O for match result: O
Enter W/L/O for match result: W
Enter W/L/O for match result: Q
Team W O L P
Two 1 1 2 3
One 2 1 1 5