在java中运行程序1000次

时间:2015-02-23 04:22:23

标签: java

我写了一个模拟3个参与者之间决斗的程序。三名参与者中的每一名在射击方面都有不同的准确性。决斗中的每个参与者轮流射击,并遵循相同的策略:每个参与者射击最有可能击中他们的人。该程序完全按照我的要求运行,直到三个中的两个退出决斗,然后打印出决斗的胜利者。

我对此程序的下一个任务是将程序打印出1000次以上,然后计算每个参与者获胜的次数。我在设置循环以打印出每个决斗时遇到了一些麻烦。我认为我应该为外循环使用for循环,但到目前为止我遇到的问题包括无限循环,或者每个决斗都有非常不一致的结果。

这是我的代码:

public class Duel {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Dueler Aaron = new Dueler();
    Dueler Bob = new Dueler();
    Dueler Charlie = new Dueler();

    Aaron.setName("Aaron");
    Aaron.setAccuracy(33);

    Bob.setName("Bob");
    Bob.setAccuracy(50);

    Charlie.setName("Charlie");
    Charlie.setAccuracy(100);

     //Aaron

        int deathCount = 0;



    while(deathCount < 2 ){

    if(Aaron.isLiving() == true && Charlie.isLiving() == true){
        System.out.println("Aaron shoots first");
        System.out.println("Aaron shoots at Charlie");
        Aaron.shootAtTarget(Charlie);
        if(Charlie.isLiving()==false){
            System.out.println("Charlie is hit");
            System.out.println("Charlie is out");
                deathCount++;
        }


        if(Charlie.isLiving() == true){
            System.out.println("miss");
    }

    }       

    else if(Aaron.isLiving() == true && Charlie.isLiving() == false){
        System.out.println("Aaron shoots at Bob");
        Aaron.shootAtTarget(Bob);
        if(Bob.isLiving() == false){
            System.out.println("Bob is hit");
            System.out.println("Bob is out");
            deathCount++;   
        }

        if(Bob.isLiving() == true){
            System.out.println("miss");
        }
    }


        System.out.println(" ");

        //Bob
        System.out.println("Bob shoots next");
        if(Bob.isLiving() == false){
            System.out.println("Bob has already been hit");
        }

        if(Bob.isLiving() == true && Charlie.isLiving() == true){
            System.out.println("Bob shoots at Charlie");
            Bob.shootAtTarget(Charlie);

            if(Charlie.isLiving() == false ){
                System.out.println("Charlie is hit");
                System.out.println("Charlie is out");
                deathCount++;
            }
            if(Charlie.isLiving() == true){
                System.out.println("miss");
            }
        }

        else if(Bob.isLiving() == true && Charlie.isLiving() == false){
            System.out.println("Bob shoots at Aaron");
            Bob.shootAtTarget(Aaron);
            if(Aaron.isLiving() == false){
                System.out.println("Aaron is hit");
                System.out.println("Aaron is out");
                deathCount++;
        }
            if(Aaron.isLiving() == true){
                System.out.println("miss");
            }

    }   

    //Charlie
            System.out.println(" ");
            System.out.println("Charlie shoots last");

            if(Charlie.isLiving() == false){
                System.out.println("Charlie has already been hit");
            }

            else if(Charlie.isLiving() == true && Bob.isLiving() == true){
                System.out.println("Charlie shoots at Bob");
                Charlie.shootAtTarget(Bob);
                if(Bob.isLiving() == false){
                    System.out.println("Bob is hit");
                    System.out.println("Bob is out");
                    deathCount++;
                }
            }
                if(Aaron.isLiving() == false && Bob.isLiving() == false){
                    System.out.println("Charlie is the winner");
                    System.out.println(" ");


                    }
                    else if(Aaron.isLiving() == false && Charlie.isLiving() == false){
                        System.out.println("Bob is the winner");
                        System.out.println(" ");


                    }
                    else if(Bob.isLiving() == false && Charlie.isLiving()     ==  false){
                        System.out.println("Aaron is the winner");
                        System.out.println(" ");



                        }



                    }



                    }

谢谢大家的帮助!

1 个答案:

答案 0 :(得分:4)

最简单的方法是使用for循环。

public static void main(String[] args){
    for(int i = 0; i < 1000; i++){
    //Insert your code
    }
}

之后,你想拥有每个人赢了多少次的全局变量。

int aaronWins = 0;
int charlieWins = 0;
int bobWins = 0;

当您检查获胜者时,您将根据赢得的人数在总胜场中添加一个。 (伪代码)

if Aaron won:
    aaronWins++

for循环之外,您可以打印最赢的人。 (伪代码)

if aaronWins > charlieWins && aaronWins > bobWins:
    print "Aaron won the most!"