1

在我的程序中,应该创建 13 个数组,以保存范围 2-12 的总和,在这种情况下,两个骰子滚动了 1000 次,并且应该打印出骰子添加到滚动的次数“2 ”。但是,最初该程序可以工作,但后来意识到我需要在程序中使用数组,而不仅仅是使用单个 math.random 方法。我已经尝试过,在 main 方法中简单地打印数组值,但出现更多错误。此外,我研究了使用直方图调用主数组的方法,除了我之前的尝试,它产生了更多的错误

我的问题是;

1:我将如何修复主要错误,允许它从 Boolean 转换为 int

2:return 语句如何工作,与常规整数相比,数组是否必须有所不同

任何指导或信息将不胜感激。

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.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");
    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]; 
    }
}
}
4

2 回答 2

1
Sixside = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1) ;

if(Sixside == 4)
   System.out.println("Print something");

我假设您要检查条件并打印。

counts[i] = Sixside;

您在 for 循环终止后使用上面的代码。i 的范围仅在 for 循环中,因为它在 for 循环中声明。所以你得到错误找不到符号变量 i

于 2013-03-20T13:38:11.457 回答
1

要将布尔值转换为 int:

一般来说,假设你有一些 boolean b,我会使用这个:

int x = b? 1:0; // If b is true, x is now 1; if b is false, x is now 0.

这使用了三元运算符

但是,在您的情况下,这种结构是不必要的。如果我理解正确,您希望 index iofcounts保存骰子总和到该索引的次数。要做到这一点:

int sumOfDice = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1);
counts[sumOfDice]++;

要返回一个数组:

返回一个数组很像返回一个 int。只需将 'input()' 的方法声明更改为

public static int[] input () throws IOException {

和返回语句

return counts;

打印一个数组:

导入java.util.Arrays并调用Arrays.toString(yourArrayHere).

完整的程序现在看起来像:

import java.io.*;
import java.util.Arrays;

public class dont {

    public static void main(String[] args) throws Exception  {
        int[] counts = input();

        System.out.println("The number of times it rolls 4 on two 6  sided dice :" + counts[4]);
        System.out.println("The number of times each number was the sum:" + Arrays.toString(counts);
    }

    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"); // We don't actually roll any eleven-sided dice, so I'm not sure why this is here?
        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();
        for (int i = 0; i < 1000; i++) 
        {
            int sumOfDice = (int)(Math.random ()*6+1) + (int)(Math.random ()*6+1);
            counts[sumOfDice]++;
        }

        // return array to main
        return counts;
    }
}
于 2013-03-20T14:20:48.033 回答