2013-03-20 65 views
1

在我的程序中,应该创建13个数组,以保持范围2-12的总和,在这种情况下,将滚动两个模具卷1000时间,并且应该打印出死亡加入的卷数“2”。然而,最初该程序工作,但后来意识到我需要在程序中使用数组,而不是仅使用一个math.random方法。我已经尝试过,只是在main方法中打印数组值,除了进行更多的错误。此外,我研究了使用直方图来调用数组,除了我以前的尝试,它创造了严重的更多的错误类型不匹配不能从布尔转换为Int(自定义方法中的数组)

我的问题是;

1:我将如何解决的主要误差,允许它从布尔转换为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]; 
    } 
} 
} 

回答

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

然而在你的情况下,这种结构是不必要的。如果我理解正确,你需要索引icounts来保存骰子加到该索引的次数。要做到这一点:

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

要返回数组:

返回数组很像返回一个int。只是改变了方法声明 '输入()' 到

public static int[] input() throws IOException { 

和return语句

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

在行int [] counts = input();我得到错误不兼容的类型 – user2184171 2013-03-20 14:42:18

+0

@ user2184171糟糕。我忘了更改方法声明。编辑。 – raptortech97 2013-03-20 15:09:10

+0

如果我添加了一个11面的骰子,我会粘贴除了(int)(Math.random()* 11 + 1)以外的其他东西,然后用一个11面骰子计算它滚动的次数2吗? – user2184171 2013-03-20 15:43:16

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

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

我假设你要检查的条件和打印。

counts[i] = Sixside; 

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

+0

仍然得到同样的错误。 – user2184171 2013-03-20 13:58:40

+0

@ user2184171你在哪一行出错? – 2013-03-20 14:00:32

+0

我在随机语句后复制了精确的代码,并收到了错误“Incompatable types,found boolean requires int”for the Line Sixside =(int)(Math.random()* 6 + 1)+(int)(Math。 random()* 6 + 1); 也错误找不到符号变量我 – user2184171 2013-03-20 14:01:47

0

我有点困惑你想要程序做什么。根据我所收集到的数据,您要将两个骰子滚动1,000次,然后存储每个结果出现在数组中的次数。如果是这样的话你的代码可能会是这个样子:

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; 

如果你想测试他们总共四个的次数,你应该使用if语句每次sum == 4递增计数。在这种情况下,你会返回一个int而不是数组。

+0

你打印的第一个代码,只是在第二个方法?是的,这是我想要做的 – user2184171 2013-03-20 14:21:20

+0

另外我得到了返回语句的错误incompatable类型。 – user2184171 2013-03-20 14:24:46

相关问题