簡単なサッカー シミュレーション プログラムを作成しようとしていますが、チーム同士のマッチングに問題があります。if ステートメントは、1 つの条件を見つけた後にループを終了します。しかし、私は2つの条件と2つの操作をしたいです。出来ますか?
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