Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印出两个六面骰子加4的次数_Java_Arrays - Fatal编程技术网

Java 如何打印出两个六面骰子加4的次数

Java 如何打印出两个六面骰子加4的次数,java,arrays,Java,Arrays,我之前的问题与此类似,只是我没有提到我的最终目标 在这段代码中,我有一个骰子,我打印出它滚动4的次数。现在我想知道,如果我有两个骰子,都是六边的,两个骰子掷2到4的次数 但是,我需要使用数组,因为在这个任务中它是必需的。我尝试在if语句中添加另一个异常,但我一直意识到我需要在程序中实际使用数组。应该存储1000个数组,因为骰子必须滚动1000次,因此检查骰子从滚动中添加到4的次数,然后打印次数 import java.io.*; public class dont { public stati

我之前的问题与此类似,只是我没有提到我的最终目标

在这段代码中,我有一个骰子,我打印出它滚动4的次数。现在我想知道,如果我有两个骰子,都是六边的,两个骰子掷2到4的次数

但是,我需要使用数组,因为在这个任务中它是必需的。我尝试在
if
语句中添加另一个异常,但我一直意识到我需要在程序中实际使用数组。应该存储1000个数组,因为骰子必须滚动1000次,因此检查骰子从滚动中添加到4的次数,然后打印次数

import java.io.*;
public class dont {

public static void main(String[] args) throws Exception  {
// System.out.println(input());
int[] counts = new int[13];
System.out.print("The number of times it rolls 4 on two 6  sided dice :" + counts);
}

public static int input () throws IOException {
BufferedReader myInput = new BufferedReader (new InputStreamReader 
System.out.println("Hello and welcome to the program");
System.out.println("In this program two six sided dices will be rolled and one eleven
sided dice will be rolled (1000 times each");


    int sum;
int[] counts = new int[13];

System.out.println("The dices will be rolled to determine the odds of how many times the roll 2 comes up on both dies(Press any key to con't) ");
myInput.readLine();
//int count2=0;
int Sixside;
for (int i = 0; i < 1000; i++) 
{
    // two dice that add to 4, after being rolled one thousand times  
    Sixside = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1) == 4;  
    //print the number of times they add to 4
    counts[sum]++;


}

counts[i] = Sixside;
{
    //return array to main
    return counts [13]; 
}
}
}
import java.io.*;
公务舱{
公共静态void main(字符串[]args)引发异常{
//System.out.println(input());
int[]计数=新的int[13];
系统输出打印(“它在两个6面骰子上滚动4次的次数:+计数);
}
公共静态int输入()引发IOException{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader
System.out.println(“您好,欢迎来到这个节目”);
在这个程序中,两个六面骰子将被滚动,一个是十一面骰子
侧面骰子将滚动(每个骰子1000次);
整数和;
int[]计数=新的int[13];
System.out.println(“掷骰子将决定掷骰2在两个骰子上出现多少次的几率(按任意键表示不)”;
myInput.readLine();
//int count2=0;
六方整数;
对于(int i=0;i<1000;i++)
{
//两个骰子,滚一千次后加成4
Sixside=(int)(Math.random()*6+1)+(int)(Math.random()*6+1)==4;
//打印它们添加到4的次数
计数[和]+;
}
计数[i]=六边;
{
//将数组返回到主
返回计数[13];
}
}
}

您的示例为总和为4的两个骰子生成了合理的答案。我怀疑您应该创建一个数组,该数组可以容纳总和在2到12之间的任何一对骰子的总和,例如:

int[] counts = new int[13];
counts[sum]++;
这样,循环中就不需要
if
语句;只需增加该总和的计数,例如:

int[] counts = new int[13];
counts[sum]++;
这可以帮助您确定其他
计数是否合理

附录:以下是您的方法的简化版本:

public static int[] input() {
    int[] counts = new int[13];
    for (int i = 0; i < 1000; i++) {
        int sum = // your expression for the sum of two dice
        counts[sum]++;
    }
    return counts;
}

两个骰子的总和永远不会是1。如果你想要两边都是4,你需要检查每个值。注意:两个4的赔率是1/36。对不起,我想在标题中加上4,我会马上改变它!!两个骰子的总和是4=>。@user2184171数组应该如何使用?数组中应该包含什么数据?假设有1000 ar射线存储,因为骰子必须掷1000次,我是否也会放置一个return counts语句,而不是return count4?是的,但您也需要更改方法的返回类型。我该如何确切地做到这一点?在“(int)(Math.random()*6+1)+(int)(Math.random()*6+1)行==4;,我得到错误类型不匹配:无法从boolean转换为int,并且我返回返回计数[13];我已经在上面详细说明了;如果您编辑您的问题,请在这里提及,并确保代码编译。