Java 程序掷骰子的次数(使用数组),并且必须显示掷骰子的次数(除非它不起作用))

Java 程序掷骰子的次数(使用数组),并且必须显示掷骰子的次数(除非它不起作用)),java,arrays,methods,Java,Arrays,Methods,我的程序的目标是掷两个六面骰子,然后将两个骰子相加(1000次),并打印出程序中掷出的数字“4”的多少倍。我尝试在循环内外使用math.random类,但没有明显区别,甚至没有使用for循环,我的目标是最终调用main方法的输出以打印它。我听到的count4++将适用于此类操作,但有一些错误导致我使用它。任何帮助、指导或建议都将不胜感激。我很抱歉没有最好的代码编写格式、技能或常识,请注意,这是我第一年学习编程。 我收到错误计数4++;无法解决,唯一的解决方法是将其设置为0,这会破坏我的程序,因为

我的程序的目标是掷两个六面骰子,然后将两个骰子相加(1000次),并打印出程序中掷出的数字“4”的多少倍。我尝试在循环内外使用math.random类,但没有明显区别,甚至没有使用for循环,我的目标是最终调用main方法的输出以打印它。我听到的count4++将适用于此类操作,但有一些错误导致我使用它。任何帮助、指导或建议都将不胜感激。我很抱歉没有最好的代码编写格式、技能或常识,请注意,这是我第一年学习编程。
我收到错误计数4++;无法解决,唯一的解决方法是将其设置为0,这会破坏我的程序,因为它总是打印0

import java.io.*;

public class Delt {


public static void main (String args [] ) throws IOException {

int i; 
int Sixside1;
int Sixside2;
int count4 = 0;
int [] data = new int [1000];
input (data);

System.out.println("The number of times '4' came up in the six sided dices is :" + count4);




}
public static void input (int num []) throws IOException {
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
String input;

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

System.out.println("The dices will be rolled to determine the odds of how many times the roll 4 comes up on both dies(Press any key to con't) ");
input = myInput.readLine ();



for (int i = 0; i < 1000; i++) 
{
  int Sixside1 = (int) (Math.random ()*(6-4) + 4); 
  int Sixside2 = (int) (Math.random ()*(6-4) + 4); 

  double total = Sixside1 + Sixside2;
  if ( total == 4) {
    // print amount of times 4 shows up in 1000 rolls, ?? 
    count4++;
    //return it to main method??
  }
}
} }
import java.io.*;
公共级三角洲{
公共静态void main(字符串args[])引发IOException{
int i;
六方会谈1;
国际六方会谈2;
int count4=0;
int[]数据=新的int[1000];
输入(数据);
System.out.println(“六边形骰子中出现'4'的次数为:“+count4”);
}
公共静态void输入(int num[])引发IOException{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader(System.in));
字符串输入;
System.out.println(“您好,欢迎来到这个节目”);
在这个程序中,两个六面骰子将被滚动,一个十一面骰子将被滚动(每个1000次);
System.out.println(“掷骰子将决定掷骰4在两个骰子上出现多少次的几率(按任意键表示不)”;
input=myInput.readLine();
对于(int i=0;i<1000;i++)
{
intsixside1=(int)(Math.random()*(6-4)+4);
intsixside2=(int)(Math.random()*(6-4)+4);
双倍合计=六边1+六边2;
如果(总数=4){
//打印次数4以1000卷显示??
count4++;
//将其返回到主方法??
}
}
} }

您没有初始化局部变量
count4
——这是必须完成的。在循环之前,您可以有:
int count4=0;
。一个方法中的局部变量与另一个方法中的局部变量不同,因此我建议您从
输入()返回
count4
变量
method到main方法,然后打印出来

你也没有像你想象的那样计算模具滚动,这意味着你永远不会得到4的和。
Math.random()
返回一个介于0和1之间的随机数(排除)-因此你的模具将是:(int)(0-0.999)*2+4=(int)(0-1.999)+4=(int)4-5.9999=4-5。相反,使用
(int)Math.random()*6+1

编辑:

publicstaticvoidmain(字符串[]args)引发异常{
System.out.println(input());
}
公共静态int输入()引发IOException{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“您好,欢迎来到这个节目”);
在这个程序中,两个六面骰子将被滚动,一个十一面骰子将被滚动(每个1000次);
System.out.println(“掷骰子将决定掷骰4在两个骰子上出现多少次的几率(按任意键表示不)”;
myInput.readLine();
int count4=0;
对于(int i=0;i<1000;i++)
{
如果((int)(Math.random()*6+1)+(int)(Math.random()*6+1)==4){
count4++;
}
}
返回计数4;
} 

您能定义“某些错误”吗请-使用堆栈跟踪和错误发生的位置。我收到错误count4++;无法解决,唯一的修复方法是将其设置为0,这会破坏我的程序,因为它总是打印0。您需要在
输入
方法中将
count4
声明为变量。仅声明它将要求我在初始化后对其进行初始化我把count4初始化为0,每次我运行程序时,它都会打印出0。我看不出你的总数会比8少多少。正如我之前所说,将count4初始化为0,每次我运行程序时都打印出0!(我刚刚尝试了你的两个建议)正如我在回答中所说,这与两个原因有关:一,你永远不会得到4,因为你的模具辊有错误。二,因为你从来没有打印实际变量。但我在主方法中打印变量,你建议我在自定义方法中再次打印吗?@user2184171它们是不同的变量。要从方法,将void替换为int-并在方法末尾添加一行
return count4;
-这样方法将把int返回到主方法。每次程序运行时,我仍然会将它打印为0。
public static void main(String[] args) throws Exception  {
    System.out.println(input());
}

public static int input () throws IOException {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
    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");

    System.out.println("The dices will be rolled to determine the odds of how many times the roll 4 comes up on both dies(Press any key to con't) ");
    myInput.readLine();
    int count4=0;
    for (int i = 0; i < 1000; i++) 
    {
        if ( (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1) == 4) {
            count4++;
        }
    }
    return count4;
}