有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用数组在两个组之间进行java IPL匹配

如果有人能帮助我使用二维数组概念,而不是使用集合,那就太好了。 因为我必须在这个逻辑中使用数组并获得输出

问题:

第一组有四个队,分别是('A'、'B'、'C'、'D') 第二组有四支队伍,分别是‘E’、‘F’、‘G’、‘H’)

用户必须选择两支球队(一支来自第一组,另一支来自第二组)

条件1:(逻辑写在下面,工作正常) 但是来自同一组的球队不应该比赛(例如:“A”和“B”队不应该比赛)

条件2:(逻辑写在下面,工作正常) 不同组别的球队可以比赛(例如:“A”队和“E”队可以比赛)

条件3:(请在这个逻辑上帮助我) 不同组别的球队不能比赛超过2次。 (例如:“A”队和“E”队只能打两次)。 第三次我们不应该允许他们玩

节目:

package demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class Ipl {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        String[] teamOne = new String[] {"A","B","C","D"};
        String[] teamTwo = new String[] {"E","F","G","H"};
        boolean flagA = false;
        boolean flagB = false;
        String teamA = null;
        String teamB = null;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        for(int l=0; l< 5 ;l++) {

            System.out.println("*******IPL MATCH 2019**********");
            System.out.println("Group A");
            for(String a: teamOne ){
                System.out.println(a);
            }
            System.out.println("Select one team from group A ");
            teamA = br.readLine();


            System.out.println("Group B");
            for(String b: teamTwo ){
                System.out.println(b);
            }
            System.out.println("Select one team from group B ");
            teamB = br.readLine();


            flagA = contains(teamOne,teamA);
            flagB = contains(teamTwo,teamB);

            if( flagA && flagB ){

                for(int i=0 ; i < teamOne.length ; i++){
                    if(teamOne[i] != null){
                        if((teamOne[i].equals(teamA)) && (teamOne[i].equals(teamB))){
                            throw new Exception("Both the team belongs to the same group");
                        }

                    }
                }

                for(int k=0 ; k < teamTwo.length ; k++){
                    if(teamTwo[k] != null){
                        if((teamTwo[k].equals(teamA)) && (teamTwo[k].equals(teamB))){
                            throw new Exception("Both the team belongs to the same group");
                        }
                    }
                }
                System.out.println("Both Team belongs to the different groups, hence they can play");

            } else {
                System.out.println("Please select teams from the availability");
            }

        } 
    }

    private static boolean contains(String[] teams, String team) {
        // TODO Auto-generated method stub
        boolean flag = false;
        for(String select : teams) {
            if( select.equals(team)){
                flag = true;
            }
        }
        return flag;
    }

}

共 (1) 个答案

  1. # 1 楼答案

    package demo;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.List;
    
    public class Ipl {
    
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
    
            String[] teamOne = new String[] { "A", "B", "C", "D" };
            String[] teamTwo = new String[] { "E", "F", "G", "H" };
            String[] fixtures = new String[10];
            int fixtureCount = 0;
            boolean flagA = false;
            boolean flagB = false;
            String teamA = null;
            String teamB = null;
    
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
            for (int l = 0; l < 5; l++) {
    
                System.out.println("*******IPL MATCH 2019**********");
                System.out.println("Group A");
                for (String a : teamOne) {
                    System.out.println(a);
                }
                System.out.println("Select one team from group A ");
                teamA = br.readLine();
    
                System.out.println("Group B");
                for (String b : teamTwo) {
                    System.out.println(b);
                }
                System.out.println("Select one team from group B ");
                teamB = br.readLine();
    
                flagA = contains(teamOne, teamA);
                flagB = contains(teamTwo, teamB);
    
                if (flagA && flagB) {
                    for (int i = 0; i < teamOne.length; i++) {
                        if (teamOne[i] != null) {
                            if ((teamOne[i].equals(teamA)) && (teamOne[i].equals(teamB))) {
                                throw new Exception("Both the team belongs to the same group");
                            }
    
                        }
                    }
    
                    for (int k = 0; k < teamTwo.length; k++) {
                        if (teamTwo[k] != null) {
                            if ((teamTwo[k].equals(teamA)) && (teamTwo[k].equals(teamB))) {
                                throw new Exception("Both the team belongs to the same group");
                            }
                        }
                    }
    
                    int matches=0;
                    for (int j=0; j<fixtureCount; j++) {
                        if( fixtures[j].equalsIgnoreCase(teamA+":"+teamB)){
                            matches++;
                        }
                    } 
                    if(matches == 2){
                        System.out.println("Already two matches fixed for the same team, please select a different combination");
                    }
                    else{
                        fixtures[fixtureCount++]= teamA+":"+teamB;
                        System.out.println("Both Team belongs to the different groups, hence they can play");
                    }                                                              
                } else {
                    System.out.println("Please select teams from the availability");
                }
                System.out.println("Available fixtures :");
                for (int j=0; j<fixtureCount; j++) {
                    System.out.println(fixtures[j].replace(":", " vs "));
                } 
            }
        }
    
        private static boolean contains(String[] teams, String team) {
            // TODO Auto-generated method stub
            boolean flag = false;
            for (String select : teams) {
                if (select.equals(team)) {
                    flag = true;
                }
            }
            return flag;
        }
    
    }