类型不匹配无法从布尔值转换为Int(自定义方法中的数组)

时间:2013-03-20 13:30:00

标签: java arrays return

在我的程序中,应该创建13个阵列,以保持2-12的总和,在这种情况下,两个掷骰子被滚动1000次,并且应该打印出骰子添加到的数量。滚动“2”。然而,最初程序工作,但后来意识到我需要在程序中使用数组,而不是只使用一个math.random方法。我尝试过,只需在main方法中打印数组值,除了进行更多错误。此外,我研究了直方图的使用来将数组调用到主数据库,除了我以前的尝试,它创建了大量的更多错误

我的问题是;

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

2:返回语句如何工作,与常规整数相比,数组必须不同

任何指导或信息都将受到赞赏。

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]; 
    }
}
}

3 个答案:

答案 0 :(得分:1)

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

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

我认为您要检查条件并打印。

counts[i] = Sixside;

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

答案 1 :(得分:1)

要从布尔值转换为int:

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

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

这使用ternary operator

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

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;
    }
}

答案 2 :(得分:0)

我有点困惑你想要程序做什么。从我收集的内容中你想要掷两个骰子1000次,然后存储每个结果在阵列中出现的次数。如果是这种情况,您的代码可能看起来像这样:

int sum;
int[] counts = new int[13];
for(int i = 0; i < 1000; i++) {
    sum = (int)(Math.random()*6+1) + (int)(Math.random()*6+1);
    counts[sum]++;
}
return counts;

如果要测试它们总数为4的次数,则应每次使用if语句递增计数sum == 4。在这种情况下,您将返回一个int而不是一个数组。