gpt4 book ai didi

java - 使用数组在两组之间进行 IPL 比赛

转载 作者:行者123 更新时间:2023-12-01 19:20:29 25 4
gpt4 key购买 nike

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

问题:

第 1 组有四支球队,名称分别为(“A”、“B”、“C”、“D”)第 2 组有四支球队,名称分别为('E'、'F'、'G'、'H')

用户必须选择两支球队进行比赛(一支来自第 1 组,另一支来自第 2 组)。

条件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;
}

}

最佳答案

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

}

关于java - 使用数组在两组之间进行 IPL 比赛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com