java - 掷骰子模拟游戏java

标签 java

我的双骰子模拟游戏程序需要帮助。掷骰子游戏的规则可以很容易地从 Google 找到。

这是我到目前为止所拥有的:

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. 如何创建变量“点”来继续滚动?
  4. 如何要求用户输入轮数“n”?
  5. 如何从命令行获取少量用户输入,作为在我的主方法中创建扫描程序的替代方法?

最佳答案

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

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

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

否则,由于除以整数,您的结果将会四舍五入,请参阅 Dividing two integers in Java gives me 0 or 100?

关于java - 掷骰子模拟游戏java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30332938/

相关文章:

java - 如何在java类中获取图层列表项ID?

java - 数组列表的比较

java - 可以在 catch block 中访问 try block 内定义的标签吗?

java - Commons-vfs 模拟一个文件系统

java - HBase读取数据返回null

java - Java 中将 HashMap 列表转换为带有换行符的 JSON 字符串

java - 使用 apache POI 在 IDE 中无法识别 workbook.createSheet() 方法

java - ArrayBlockingQueue 和添加 vs 放置 vs 容量

java - 在 android 中调整图片大小同时仍保持质量

java - 限制函数调用的时间