使用数组在两个组之间进行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 楼答案