简体   繁体   中英

BoxCars Java Program - Beginner. Weird Issue: Program doesn't run in the console but, it lists no errors in Eclipse

I wrote this code in Eclipse Java and for some reason, it doesn't run. It doesn't say it has any errors in it and no red marks appear anywhere in the code. I'm not sure what is wrong with it, please help.

Here is the description of what I needed to write: Design and implement a class called PairOfDice, composed of two six-sided Die objects. Create a driver class called BoxCars with a main method that rolls a PairOfDice object 1000 times, counting the number of boxcars (two sixes) that occur.

Another issue I have is creating "two six-sided Die objects" in the PairOfDice class. I don't have it written in the code, so if someone could explain how to implement those objects I would appreciate it.

The last issue I'm having is making a driver class (BoxCars). I have tried to look up what exactly was a driver class but I couldn't find anything that I could understand.

public class dieGames {

    public class PairOfDice {

       private int die1; 
       private int die2;

       public PairOfDice() {
           roll();
       }

       public void roll() {
          die1 = (int)(Math.random()*6) + 1;
          die2 = (int)(Math.random()*6) + 1;
       }

       public int getValueDie1() {
          return die1;
       }

       public int getValueDie2() {
          return die2;
       }

       public String toString() {
          return "Die 1: " + die1 + ", Die 2: " + die2;
       }
    }

    public class BoxCars
    {
       public void main(String[] args)
       {
          final int numRolls = 1000;
          int numBoxCars = 0;

          PairOfDice twoDice = new PairOfDice();

          for (int i = 0; i < numRolls; i++)
          {
             twoDice.roll();
             if (twoDice.die1 == 6 && twoDice.die2 == 6)
             {
                numBoxCars++;
             }
          }

          System.out.println("Number of Box Cars in " + numRolls +
                             " rolls is " + numBoxCars);
       }
    }
}

Why is BoxCars inside the PairOfDice class? They should be seperated files. Also the assignment says something about 'Die objects', which makes me question where your Die class is.

The code itself looks like it should work and should give the correct answer though (I haven't tried it to run).

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