Craps模拟游戏java

时间:2015-05-19 18:03:44

标签: java

我的Craps模拟游戏程序需要帮助。可以从谷歌轻松找到掷骰子游戏的规则。

这是我到目前为止所做的:

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) + "%");

     }
   }
}

总结我的问题是:

  1. 我是否代表了一个2到12之间的随机数,以便在roll()方法中正确地刺激掷骰子?
  2. 我如何编写一个while循环来重复我在round()方法中的if-else循环决策以允许额外的滚动?
  3. 如何创建变量&#34; point&#34;继续玩滚动?
  4. 如何让用户输入轮数&#34; n&#34;?
  5. 如何从命令行获取少量用户输入,作为在main方法中创建Scanner的替代方法?

1 个答案:

答案 0 :(得分:1)

  1. 要获取命令行输入,请参阅https://stackoverflow.com/questions/17501509/how-to-get-user-input/17501524#17501524

  2. 首先获得100倍的百分比:

    System.out.println("Winning percentage: " + (100 * wins / n)  + "%");
    
  3. 否则,由于除以整数,您将得到舍入结果,请参阅Dividing two integers in Java gives me 0 or 100?

相关问题