1

我正在尝试制作一个简单的足球模拟程序,但我在匹配球队时遇到了问题。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

4

5 回答 5

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)。

于 2013-11-03T10:13:44.260 回答
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);

        }   
    }
于 2013-11-03T10:14:48.157 回答
0

我会从头开始。你想做什么。

给定一些球队,您想选择两支随机球队进行比赛,一支主队和一支客队。

当前,您正在从 a 中选择两个随机intID,Random并使用它们在隐含的团队数组中查找团队。

因此,首先采用@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

主要的

接下来我们有 outmain方法。此方法获取团队并将它们读入List. 我们需要制作团队的副本,这样我们就不会弄乱团队数组。AList是一种更灵活的结构,稍后您将看到。另请注意,您需要两个团队的力量 - 如果团队成对比赛,那么您需要在每轮中被二整除的数字。

该方法现在进入一个while循环。这种结构让每轮比赛的所有获胜球队都相互对抗,直到最终获胜者——锦标赛冠军。它打印出冠军的名字。

游戏轮

这就是魔法发生的地方。

该方法首先shuffles传入团队,以便随机检索它们。然后它会得到Iteratorfor the - 这使您可以逐个元素List地行走。List如果团队数量不是偶数,这也是引发错误的地方 - 第二个next()将引发NoSuchElementException.

然后该方法进入另一个while循环 - 虽然Iterator不是空的,但它play与两个团队一起调用。它将获胜的团队放入winners List. 这是List最后返回的那个。

并不是说 aList有一个add方法可以动态填充它 - 这与具有固定大小的数组不同。

这是您实际模拟的地方。目前它随机选择一个获胜者 - 你可以输入你当前的方法。

于 2013-11-03T10:28:21.447 回答
0

用if替换else if,代码应该可以正常工作。

于 2013-11-03T10:16:02.007 回答
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";
            }
        }

你总是只分配一支球队,无论是主队还是客队,因为你在这里使用了错误的结构。尝试类似:

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代码约定。变量名应该以小写字母开头。

于 2013-11-03T10:13:59.193 回答