我将如何处理并行数组以在Java中存储不同类型的信息

hussain123:

我有以下任务:

最多可能有十支队伍。并行数组用于存储团队名称,并跟踪获胜次数,加班损失和积分。输入最后一个团队的结果后,程序将按照输入顺序相反的顺序输出每个团队的记录摘要。

注意:“ 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");
            }
        }

如何将并行阵列合并到选项3中,并且具有与选项2相同的想法?对于学习并行数组,我真的很陌生,无法在线上找到有关它的任何帮助。

在此处输入图片说明

Arvind Kumar Avinash:

您需要名称,胜利,失败,加班和分数的数组。但是,当您了解更多信息时,可以通过创建自定义类型say来更好地进行管理,该自定义类型say Game将具有,等属性name,并具有List用于记录获胜,失败,加班等情况type属性。

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 i;

        if (option == 3) {
            // Label to to be used to quit as per user's choice
            exit:

            // Input and calculation
            for (i = 0; i < teams; i++) {
                String letter;
                boolean go = true;

                System.out.print("Enter the name of team " + (i + 1) + ": ");
                teamNames[i] = keyboard.next();
                while (go) {
                    System.out.print("Enter W/L/O for match result or N to end the game for this team: ");
                    letter = keyboard.next();

                    if (letter.equals("W")) {
                        wins[i]++;
                    } else if (letter.equals("O")) {
                        overtimes[i]++;
                    } else if (letter.equals("L")) {
                        losses[i]++;
                    } else if (letter.equals("N") || letter.equals("Q")) {
                        points[i] = wins[i] * 2 + overtimes[i];
                        go = false;
                        if (letter.equals("Q")) {
                            break exit;
                        }
                    }
                }
            }

            // Output
            System.out.println("Team W O L P");
            for (int j = i; j >= 0; j--) {
                System.out
                        .println(teamNames[j] + " " + wins[j] + " " + overtimes[j] + " " + losses[j] + " " + points[j]);
            }
        }
    }
}

运行示例:

Enter option: 3
Enter the name of team 1: One
Enter W/L/O for match result or N to end the game for this team: W
Enter W/L/O for match result or N to end the game for this team: W
Enter W/L/O for match result or N to end the game for this team: L
Enter W/L/O for match result or N to end the game for this team: O
Enter W/L/O for match result or N to end the game for this team: N
Enter the name of team 2: Two
Enter W/L/O for match result or N to end the game for this team: L
Enter W/L/O for match result or N to end the game for this team: L
Enter W/L/O for match result or N to end the game for this team: O
Enter W/L/O for match result or N to end the game for this team: W
Enter W/L/O for match result or N to end the game for this team: Q
Team W O L P
Two 1 1 2 3
One 2 1 1 5

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何处理 LINQ 中的并行数组?

如何处理不同类型的列表

如何处理不同类型的物品

如何在java中处理不同类型的输入

如何处理不同类型的算法输入?

Java如何存储不同类型的对象

如何处理带有数据框的列中的不同类型

从txt文件到数组Java存储不同类型的类型

我应该如何处理Java中的大型数组?

如何在Swift中为UITableView处理数组中的两种不同类型

如何并行查询不同类型的文档?

如何从JSON对象数组中检索不同类型的数据并进行处理?

在Java中的列表中存储不同类型的元素

如何在不同类型的 Java 中创建一个包含 2 个数组的数组

处理Java中相同类型的不同异常?

如何在C中存储不同类型的数据?

如何在容器中存储不同类型的对象?

如何在Lucene中存储多种不同类型的文档

如何使功能可以在OpenCV中处理不同类型的图像

如何在字典中存储来自 C# 中不同类的不同类型的方法

如何处理在网络上传递不同类型的序列化消息

格森:如何处理可能具有不同类型的字段?

如何处理来自同一个异常对象的不同类型的异常?

如何处理来自可观察对象的不同类型的数据?

如何从Java中的并行数组中删除重复项?

如何使用Object类型的数组访问Java中不同类的方法

在Typescript中,什么是PoorMansUnknown = {} |类型?空| 未定义,您将如何处理?

我可以在Java中创建不同类型的ArrayList吗

我将如何处理“子字符串”?使用Java的JSON文件?