lundi 27 novembre 2017

Why is my code not breaking out of my loop when its suppose to and why is stuff repeating?

I am a beginner programmer so bare with me. I am currently working on a project for my coding class, where I am tasked with writing a program to simulate a batter facing a pitcher in a baseball game for one turn at bat. I think the problem in my code is a logic error, but I'm not quite sure how to fix it. I have tried added additional while loops and if statements but nothing has worked. The code in my batter class and pitcher class work completely fine, it's just the code in my driver. Stuff sometimes breaks out of loop when it's suppose to and other times it doesn't and sometimes it repeats the same line of text when it's not suppose to. I would really appreciate some help on this. Thank you so much in advance.

Here is my code.

The first part being the batter class:

public class Batter {

private String name;

private double average;

public boolean hit(){
     int rnum = (int) (Math.random() * 100 + 1);
     average = .250 * 100;


     if (rnum <= average){
         System.out.println(name + " got a hit!");
         return true;

     }

     if (rnum > average){
         System.out.println(name + " swung and missed");

     }
     return false;
}
public String getname(){

    name = "Alex Rodriguez";
    return name;
}

Here is the pitcher class:

  public class Pitcher {
 private String name;
  private double average; 

   public boolean pitch(){
   int rnum = (int) (Math.random() * 100 + 1);
   average = .80 * 100;
   if ( rnum <= average){

       return true;

   }
   if (rnum> average){
       //int ball = 0;
       //ball++;
       System.out.println(name + " threw a ball");
   }
   return false;
    }

    public String getname(){
   name = "Phil Hughes";
   return name;
   }
   }

This is the driver program:

     public static void main(String[] args) {

       Batter batter = new Batter();
    batter.getname();
    // making the pitcher object and calling the get name method.
    Pitcher pitcher = new Pitcher();
    pitcher.getname();
    System.out.println(pitcher.getname() + " is pitching to " + batter.getname());

     while (true) {
        int ball = 0;
        int strike = 0;
        // desiding if the hit method gets called on the batter object
        if (pitcher.pitch() == false) {

            ball++;

            System.out.println("The count is " + ball + " balls " + strike + " strikes");
        }
        if (pitcher.pitch()== false && ball ==4 ){
            System.out.println("The count is " + ball + " balls " + strike + " strikes");
            System.out.println(batter.getname() + " walked.");
            break;
        }

        if (batter.hit() == false) {

            strike++;
            System.out.println("The count is " + ball + " balls " + strike + " strikes");



        }
        if (batter.hit() == false && strike == 3){
            System.out.println("The count is " + ball + " balls " + strike + " strikes");
            System.out.println(batter.getname() + " struck out.");
            break;
        }

        if (batter.hit() == true && pitcher.pitch() == true) {
                break;
            }
    }
} }

This is what I get when I run my code:

Phil Hughes is pitching to Alex Rodriguez

Alex Rodriguez got a hit!

Alex Rodriguez swung and missed

Alex Rodriguez swung and missed

Phil Hughes threw a ball

The count is 1 balls 0 strikes

Alex Rodriguez swung and missed

The count is 1 balls 1 strikes

Alex Rodriguez swung and missed

Alex Rodriguez swung and missed

Alex Rodriguez swung and missed

The count is 0 balls 1 strikes

Alex Rodriguez got a hit!

Alex Rodriguez got a hit!

Aucun commentaire:

Enregistrer un commentaire