java - 骰子模拟寻找正确的点

标签 java

how many dice dots do u want?
6
try 1 : 2
try 2 : 4
try 3 : 5
try 4 : 6
In 4 try's u found 6 dots

这就是我想要获得的输出,但似乎找不到如何实现。

System.out.println("how many dice dots do u want?");
int dots = s.nextInt();
int dots2 = (int) (6 * Math.random()) + 1;

这就是我所拥有的。我现在尝试了 for 循环,但似乎不起作用。有人可以帮助我吗?

最佳答案

您可以通过以下方式实现您想要的:

Scanner scan = new Scanner (System.in);

    int randomValue = 0;   //this is the random value that'd be autogenerated in each loop
    int counter = 1;    //this represents the number of trials

    System.out.println("how many dice dots do u want?");
    int dotsWanted = scan.nextInt();

    while(randomValue != dotsWanted){
        randomValue = (int) (6 * Math.random()) + 1;
        counter++;
    }

    System.out.printf("In %d trials, %d dots were found\n", counter, dotsWanted);

我希望这会有所帮助......快乐编码!

关于java - 骰子模拟寻找正确的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48094393/

相关文章:

java - 删除数据库后未指定数据源问题 'url'属性

java - isEnabled() 方法始终返回 true

java - 如何从命令行停止 spring boot 服务?

java - Jexcel编辑现有文件

java - Checkstyle - 大括号内的空格

java - 没有找到主类?网 bean

java - SWT/JFace TextCellEditor 字段辅助不工作

java - 我在 java 中循环输入时遇到错误

java - 将 xml 代码解码为 Java 对象的最佳方法?

java - 在 java.io.FileWriter 中刷新