我正在尝试制作一个简单的足球模拟程序,但是我有一个相互匹配的问题。 if语句在找到一个条件后退出循环。但我想做两个条件和两个操作。有可能吗?
package soccer.simulator;
import java.util.Random;
/**
* @author Sertac
*/
public class SoccerSimulator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int HomeTeamScore = 0;
int AwayTeamScore = 0;
Random randomGenerator = new Random();
String HomeTeam = new String();
String AwayTeam = new String();
int HomeTeamID = randomGenerator.nextInt(10);
int AwayTeamID = randomGenerator.nextInt(10);
if(HomeTeamID == AwayTeamID){
while (HomeTeamID != AwayTeamID){
AwayTeamID = randomGenerator.nextInt(10);
}
}
if(HomeTeamID == 0 || AwayTeamID == 0){
if(HomeTeamID == 0){
HomeTeam = "Arsenal";
}else{
AwayTeam = "Arsenal";
}
} else if(HomeTeamID == 1 || AwayTeamID == 1){
if(HomeTeamID == 1){
HomeTeam = "Barcelona";
}else{
AwayTeam = "Barcelona";
}
} else if(HomeTeamID == 2 || AwayTeamID == 2){
if(HomeTeamID == 2){
HomeTeam = "Bayern Munich";
}else{
AwayTeam = "Bayern Munich";
}
} else if(HomeTeamID == 3 || AwayTeamID == 3){
if(HomeTeamID == 3){
HomeTeam = "Chelsea";
}else{
AwayTeam = "Chelsea";
}
} else if(HomeTeamID == 4 || AwayTeamID == 4){
if(HomeTeamID == 4){
HomeTeam = "Borussia Dortmund";
}else{
AwayTeam = "Borussia Dortmund";
}
} else if(HomeTeamID == 5 || AwayTeamID == 5){
if(HomeTeamID == 5){
HomeTeam = "Galatasaray";
}else{
AwayTeam = "Galatasaray";
}
} else if(HomeTeamID == 6 || AwayTeamID == 6){
if(HomeTeamID == 6){
HomeTeam = "Juventus";
}else{
AwayTeam = "Juventus";
}
} else if(HomeTeamID == 7 || AwayTeamID == 7){
if(HomeTeamID == 7){
HomeTeam = "Manchester United";
}else{
AwayTeam = "Manchester United";
}
} else if(HomeTeamID == 8 || AwayTeamID == 8){
if(HomeTeamID == 8){
HomeTeam = "Milan";
}else{
AwayTeam = "Milan";
}
} else if(HomeTeamID == 9 || AwayTeamID == 9){
if(HomeTeamID == 9){
HomeTeam = "Real Madrid";
}else{
AwayTeam = "Real Madrid";
}
}
//Generating each random integers in range 0..99 for 90 minutes
for(int minutes = 0; minutes <= 90; minutes++){
int randomInt = randomGenerator.nextInt(100);
//if random int equals 0,1,2 home team scores
if(randomInt < 3){ HomeTeamScore = HomeTeamScore + 1; }
//if random int equals 98,99 away team scores
//home team has 1 more int because playing at home is better
if(randomInt > 97){ AwayTeamScore = AwayTeamScore + 1; }
}
System.out.println ("Simulation for match of the week:");
System.out.println (HomeTeam + " " + HomeTeamScore + " - " + AwayTeamScore + " " + AwayTeam);
}
}
输出是:
3 - 1 Arsenal
或:
Arsenal 2 - 1
答案 0 :(得分:5)
您只设置一个团队名称,因为根本没有循环,在您的巨大if-else
语句中只有一个最终条件成立。一种更简单的方法(并且非常值得推荐,以便在不久的将来为其他任何会触及您的代码的人保持理智)将您的团队名称存储在一个数组中.-
String[] teamNames = new String[] {"Arsenal", "Barcelona", "Bayern Munich", "Chelsea", "Borussia Dortmund", "Galatasaray", "Juventus", "Manchester United", "Milan", "Real Madrid"};
然后将整个if-else
替换为.-
HomeTeam = teamNames[HomeTeamID];
AwayTeam = teamNames[AwayTeamID];
作为旁注,你应该坚持使用变量命名的java约定,并使用小写的camelCase(homeTeam
,awayTeam
,homeTeamID
,awayTeamId
)。< / p>
答案 1 :(得分:2)
将else if
更改为if
试
import java.util.Random;
/**
* @author Sertac
*/
public class SoccerSimulator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int HomeTeamScore = 0;
int AwayTeamScore = 0;
Random randomGenerator = new Random();
String HomeTeam = new String();
String AwayTeam = new String();
int HomeTeamID = randomGenerator.nextInt(10);
int AwayTeamID = randomGenerator.nextInt(10);
if(HomeTeamID == AwayTeamID){
while (HomeTeamID != AwayTeamID){
AwayTeamID = randomGenerator.nextInt(10);
}
}
if(HomeTeamID == 0 || AwayTeamID == 0){
if(HomeTeamID == 0){
HomeTeam = "Arsenal";
}else{
AwayTeam = "Arsenal";
}
} if(HomeTeamID == 1 || AwayTeamID == 1){
if(HomeTeamID == 1){
HomeTeam = "Barcelona";
}else{
AwayTeam = "Barcelona";
}
} if(HomeTeamID == 2 || AwayTeamID == 2){
if(HomeTeamID == 2){
HomeTeam = "Bayern Munich";
}else{
AwayTeam = "Bayern Munich";
}
} if(HomeTeamID == 3 || AwayTeamID == 3){
if(HomeTeamID == 3){
HomeTeam = "Chelsea";
}else{
AwayTeam = "Chelsea";
}
} if(HomeTeamID == 4 || AwayTeamID == 4){
if(HomeTeamID == 4){
HomeTeam = "Borussia Dortmund";
}else{
AwayTeam = "Borussia Dortmund";
}
} if(HomeTeamID == 5 || AwayTeamID == 5){
if(HomeTeamID == 5){
HomeTeam = "Galatasaray";
}else{
AwayTeam = "Galatasaray";
}
} if(HomeTeamID == 6 || AwayTeamID == 6){
if(HomeTeamID == 6){
HomeTeam = "Juventus";
}else{
AwayTeam = "Juventus";
}
} if(HomeTeamID == 7 || AwayTeamID == 7){
if(HomeTeamID == 7){
HomeTeam = "Manchester United";
}else{
AwayTeam = "Manchester United";
}
} if(HomeTeamID == 8 || AwayTeamID == 8){
if(HomeTeamID == 8){
HomeTeam = "Milan";
}else{
AwayTeam = "Milan";
}
} if(HomeTeamID == 9 || AwayTeamID == 9){
if(HomeTeamID == 9){
HomeTeam = "Real Madrid";
}else{
AwayTeam = "Real Madrid";
}
}
//Generating each random integers in range 0..99 for 90 minutes
for(int minutes = 0; minutes <= 90; minutes++){
int randomInt = randomGenerator.nextInt(100);
//if random int equals 0,1,2 home team scores
if(randomInt < 3){ HomeTeamScore = HomeTeamScore + 1; }
//if random int equals 98,99 away team scores
//home team has 1 more int because playing at home is better
if(randomInt > 97){ AwayTeamScore = AwayTeamScore + 1; }
}
System.out.println ("Simulation for match of the week:");
System.out.println (HomeTeam + " " + HomeTeamScore + " - " + AwayTeamScore + " " + AwayTeam);
}
}
答案 2 :(得分:0)
问题在于:
if(HomeTeamID == 0 || AwayTeamID == 0){
if(HomeTeamID == 0){
HomeTeam = "Arsenal";
}else{
AwayTeam = "Arsenal";
}
} else if(HomeTeamID == 1 || AwayTeamID == 1){
if(HomeTeamID == 1){
HomeTeam = "Barcelona";
}else{
AwayTeam = "Barcelona";
}
} else if(HomeTeamID == 2 || AwayTeamID == 2){
if(HomeTeamID == 2){
HomeTeam = "Bayern Munich";
}else{
AwayTeam = "Bayern Munich";
}
} else if(HomeTeamID == 3 || AwayTeamID == 3){
if(HomeTeamID == 3){
HomeTeam = "Chelsea";
}else{
AwayTeam = "Chelsea";
}
} else if(HomeTeamID == 4 || AwayTeamID == 4){
if(HomeTeamID == 4){
HomeTeam = "Borussia Dortmund";
}else{
AwayTeam = "Borussia Dortmund";
}
} else if(HomeTeamID == 5 || AwayTeamID == 5){
if(HomeTeamID == 5){
HomeTeam = "Galatasaray";
}else{
AwayTeam = "Galatasaray";
}
} else if(HomeTeamID == 6 || AwayTeamID == 6){
if(HomeTeamID == 6){
HomeTeam = "Juventus";
}else{
AwayTeam = "Juventus";
}
} else if(HomeTeamID == 7 || AwayTeamID == 7){
if(HomeTeamID == 7){
HomeTeam = "Manchester United";
}else{
AwayTeam = "Manchester United";
}
} else if(HomeTeamID == 8 || AwayTeamID == 8){
if(HomeTeamID == 8){
HomeTeam = "Milan";
}else{
AwayTeam = "Milan";
}
} else if(HomeTeamID == 9 || AwayTeamID == 9){
if(HomeTeamID == 9){
HomeTeam = "Real Madrid";
}else{
AwayTeam = "Real Madrid";
}
}
由于您在这里使用了错误的构造,因此U总是只分配一个团队,无论是家庭团队还是远程团队。尝试类似:
switch(HomeTeamID)
{
case 0: HomeTeam = "Arsenal";
break;
case 1: HomeTeam = "Barcelona";
...
break
}
和客队相同:
switch(HomeTeamID)
{
case 0: AwayTeam = "Arsenal";
break;
case 1: AwayTeam = "Barcelona";
...
break
}
你应该检查java代码约定。变量名应该以小写开头。
答案 3 :(得分:0)
如果使用if,则替换else,并且代码应该正常工作。
答案 4 :(得分:0)
我会从一开始就开始。你想做什么。
考虑到一些球队,你想选择两个随机球队互相比赛,一个回家,一个回家。
目前,您正在从int
中选择两个随机Random
ID,并使用这些ID来查找隐含的一组团队中的团队。
所以,首先使用@ssantos的建议 - 使数组显式化并简单地从那里获取索引。
但是,我们有一个问题 - 一个团队无法发挥自己。所以实际上我们需要一种稍微不同的方法。您需要做的是获取所有团队的数组,将其洗牌,然后从数组中读取对。事实上,这种选择往往会在锦标赛中发挥作用。
以下是我建议的代码:
private static final String[] TEAM_NAMES = new String[]{"Arsenal", "Barcelona", "Bayern Munich", "Chelsea", "Borussia Dortmund", "Galatasaray", "Juventus", "Manchester United", "Milan", "Real Madrid"};
private static final Random RANDOM = new Random();
public static void main(final String args[]) {
List<String> teams = new ArrayList<>(Arrays.asList(TEAM_NAMES));
while (teams.size() > 1) {
teams = playRound(teams);
}
System.out.println("The champion is " + teams);
}
public static List<String> playRound(final List<String> teams) {
Collections.shuffle(teams);
final Iterator<String> teamsIter = teams.iterator();
final List<String> winners = new ArrayList<>();
while (teamsIter.hasNext()) {
final String winner = play(teamsIter.next(), teamsIter.next());
winners.add(winner);
}
return winners;
}
public static String play(final String team1, final String team2) {
return RANDOM.nextBoolean() ? team1 : team2;
}
从顶部开始:
首先我们声明常量 - 团队数组和Random
实例。这些变量为static final
,因此它们以BLOCK_CAPITALS
命名。
主要强>
接下来我们有main
方法。这种方法需要团队并将其读入List
。我们需要制作团队的副本,这样我们就不会乱搞团队阵列。 List
是一种更灵活的结构,您稍后会看到。另请注意,你需要两支球队的力量 - 如果球队成对比赛,那么你需要一个可以在每轮中被2整除的数字。
该方法现在进入while
循环。这种结构让每一轮的所有胜利球队相互对抗,直到最终获胜者 - 锦标赛冠军。它打印出冠军的名字。
<强> playRound 强>
这就是魔术发生的地方。
方法首先shuffles
传入的团队,以便随机检索它们。然后它获取Iterator
的{{1}} - 这允许您按元素遍历List
元素。如果没有偶数的团队,这也是错误发生的地方 - 第二个List
会抛出next()
。
然后该方法进入另一个NoSuchElementException
循环 - 当while
不为空时,它会调用Iterator
两个团队。它将获奖团队纳入play
winners
。这是最后返回的List
。
不是List
有一个List
方法,以便您可以动态填充它 - 这与具有固定大小的数组不同。
播放强>
这是您实际模拟的地方。目前,它随机选择一名获胜者 - 您可以将当前的方法放入。