0

I need help with my Craps simulation game program. The rules for craps game can be easily found from Google.

This is what I have so far:

public class Craps {

    //static class variables

       static int Random, r1, r2, n;

       //declare variables to be shared between methods here

       public static Random r;
       public static Scanner in;


      static {
      r = new Random();
      in = new Scanner(System.in);
   }

   //this method returns a value between 2 and 12

   //to stimulate the roll of the two dice

   public static int roll() {

   for (int i = 1; i<= 2; i++) {

   int r= (int)(Math.random()*12); 

    System.out.println(Random);

   }

   return 0;

 }

   //this method implements the rules of the game for one round

   //it calls the roll() method to stimulate throwing the dice

   public static boolean round() {


   boolean print = false;

   //if roll 7 or 11 = win

   int roll1 = roll();

      if (print==true) System.out.print(roll1);

          if (r1==7||r1==11) {

         System.out.print("Win");

         return true;

       }

       //if roll 2, 3, 12 = lose

         else if (r1==2||r1==3||r1==12) {
         System.out.print("Lost");
         return false;
       }

    //if point equals same number as rolled the first time, win
         else {
         int r2=roll();
         int point = roll1;
       }
       return false;
   } 

   public static void main(String []args) {

   //call the "round()" method n times in a loop

   //to play n rounds

   int n = 10;
   for (int i = 1; i<=n; i++) {

   boolean result = round();

   }

   boolean print;


      //collect wins/losses at the end of rounds, output wins/n as percentage
      for (int wins=0; wins<n; wins++) { 
      System.out.println("Winning percentage: " + (100 * wins / n) + "%");

     }
   }
}

Thus summarizing my questions are:

  1. Did I represent a random number between 2 and 12 to be generated to stimulate rolling the dice correctly in roll() method?
  2. How can I write a while loop to do repetition of my if-else loop decisions in round() method to allow additional rolls?
  3. How can I create a variable "point" to continue the play of rolls?
  4. How can I ask the user to input number of rounds "n"?
  5. How to get small amounts of user input from command line as an alternative to creating a Scanner in my main method?
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • What exactly are you looking for ? Explain it by example or more detailed explanation. – Code Sniper May 19 '15 at 18:12
  • 6
    Please don't ask questions as a comment in your code. – tnw May 19 '15 at 18:13
  • 1
    Welcome to Stack Overflow, noobeginner. This site is a bit different from what you might be expecting -- it's not a discussion forum, a tutorial, nor a code review site. It's about a very particular kind of question and answer. Spend a couple of minutes looking over the "two minute tour" to get an idea of what we do here: http://stackoverflow.com/tour – RJHunter May 19 '15 at 18:36
  • 3
    @AnubianNoob this question would be off-topic on Code Review because the code does not quite work as intended yet. – RubberDuck May 19 '15 at 18:39
  • @RJHunter Yes, it can be a code review site, as long as you don't put the whole code like noobeginner did. – MCGuy Sep 28 '16 at 20:42

1 Answers1

1
  1. To get command line input see https://stackoverflow.com/questions/17501509/how-to-get-user-input/17501524#17501524

  2. To get a percentage multiple by 100 first:

    System.out.println("Winning percentage: " + (100 * wins / n)  + "%");
    

otherwise you will get your result rounded due to dividing integer numbers, see Dividing two integers in Java gives me 0 or 100?

Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141