简体   繁体   中英

run program 1000 times in java

I've written a program that simulates a duel between 3 participants. Each of the three participants has differing accuracies in their shooting. Each participant in the duel takes turns shooting, and follows the same strategy: each participant shoots at the person who has the highest chance of hitting them on their turn. The program works exactly as I want it to, running until two of the three is out of the duel, and then printing out the winner of the duel.

My next task with this program is to print the program out 1000 more times, and then to count the number of times each participant wins. I'm having some trouble getting the loop set up to print out each duel. I think that I should be using a for loop for the outer loop, but the problems I have run into so far include an infinite loop, or wildly inconsistent results on each duel.

Here is my code:

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(" ");



                        }



                    }



                    }

Thank you everyone for your help!

The easiest way would be to use a for loop.

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

After that, you want to have global variables for how many times each person has won.

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

When you check the winner, you add one to the total wins depending on who won. (Pseudocode)

if Aaron won:
    aaronWins++

And outside your for loop, you can print who won the most. (Pseudocode)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM