Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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/13.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 类型不匹配可以';t从布尔值转换为Int(自定义方法中的数组)_Java_Arrays_Return - Fatal编程技术网

Java 类型不匹配可以';t从布尔值转换为Int(自定义方法中的数组)

Java 类型不匹配可以';t从布尔值转换为Int(自定义方法中的数组),java,arrays,return,Java,Arrays,Return,在我的程序中,应该创建13个数组,以保持范围2-12的总和,在这种情况下,两个模具辊被滚动1000次,并且应该打印出模具添加到辊“2”的次数。然而,最初这个程序是有效的,但后来意识到我需要在程序中使用数组,而不仅仅是使用单一的math.random方法。我已经尝试过了,只是在main方法中打印数组值,除了会出现更多错误。此外,我还研究了直方图调用main数组的用法,除了我以前的尝试之外,它还产生了大量错误 我的问题是, 1:我如何修复主要错误,允许它从布尔值转换为int值 2:return语句是

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

我的问题是,

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

2:return语句是如何工作的?数组的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]; 
    }
}
}
import java.io.*;
公务舱{
公共静态void main(字符串[]args)引发异常{
//System.out.println(input());
int[]计数=新的int[13];
系统输出打印(“它在两个6面骰子上滚动4次的次数:+计数);
}
公共静态int输入()引发IOException{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader(System.in));
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];
}
}
}
我假设您要检查条件并打印

counts[i] = Sixside;

for循环终止后,您正在使用上述代码。i的作用域仅在for循环内,因为它在for循环中声明。因此,您找不到符号变量i,无法将其从布尔值转换为int:

通常,假设您有一些布尔值
b
,我将使用以下方法:

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

但是,在您的情况下,这种构造是不必要的。如果我理解正确,您希望index
I
of
counts
保存骰子与该索引相加的次数。为此:

int sumOfDice = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1);
counts[sumOfDice]++;
返回数组:

返回数组与返回int非常相似。只需将“input()”的方法声明更改为

以及返回语句

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;
    }
}
import java.io.*;
导入java.util.array;
公务舱{
公共静态void main(字符串[]args)引发异常{
int[]计数=输入();
System.out.println(“它在两个6面骰子上掷4的次数:“+计数[4]);
System.out.println(“每个数字等于和的次数:”+Arrays.toString(计数);
}
公共静态int[]输入()引发IOException{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“您好,欢迎来到这个节目”);
System.out.println(“在这个程序中,两个六面骰子将被滚动,一个十一面骰子将被滚动(每个1000次”);//我们实际上没有滚动任何十一面骰子,所以我不确定为什么会出现这种情况?
int[]计数=新的int[13];
System.out.println(“掷骰子将决定掷骰2在两个骰子上出现多少次的几率(按任意键表示不)”;
myInput.readLine();
对于(int i=0;i<1000;i++)
{
int sumOfDice=(int)(Math.random()*6+1)+(int)(Math.random()*6+1);
计数[总和]+;
}
//将数组返回到主
返回计数;
}
}

我在random语句之后复制了该精确代码,并收到了错误“不兼容类型,发现布尔值需要int”,该错误出现在第六行=(int)(Math.random()*6+1)+(int)(Math.random()*6+1);并且该错误在第int[]行counts=input()中找不到符号变量;我得到了错误types@user2184171哎呀,我忘了更改方法声明。已编辑。如果我添加了一个十一面骰子,除了do(int)(Math.random()*11+1)之外,我还会粘贴相同的东西吗取而代之的是,计算它用一个11面骰子掷2的次数?@user2184171听起来不错,不过你应该检查一下,你不想让它用11面骰子掷4的次数(尽管另一方面,它们无论如何都应该是相同的)。那么这是计数[2]而不是4吗?
return counts;
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;
    }
}