問題描述
Loại Không khớp không thể chuyển đổi từ Boolean sang Int (Mảng trong phương thức tùy chỉnh) (Type Mismatch can't convert from Boolean to Int (Arrays in custom method))
In my program 13 arrays are supposed to be created, to hold a sum of the range 2‑12, in this case two die rolls are rolled 1000 times, and are supposed to print out the amount of times the die's add to roll "2". However,originally the program worked, but later realizing that I need to use arrays in the program, instead of just using a single math.random method. I have tried, simply printing the array values, in the main method, except that proceeded with more errors. Also, i researched the usage of histogram to call arrays to the main, except as my previous attempt, it created severe amounts of more errors
My question is ;
1: How would I fix the main error, allow it to convert from Boolean to int
2: How does a return statement work, does it have to be different for arrays compared to regular integers
Any guidance or information would be well appreciated.
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:
Sixside = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1) ;
if(Sixside == 4)
System.out.println("Print something");
I assume that you want to check condition and print .
counts[i] = Sixside;
You are using above code after for loop terminates. Scope of i is inside for loop only as it is declared in for loop . So you getting error cannot find symbol variable i
方法 2:
To convert from boolean to int:
In general, assuming you have some boolean b
, I would use this:
int x = b? 1:0; // If b is true, x is now 1; if b is false, x is now 0.
This uses the ternary operator.
In your case, however, this construction is unnecessary. If I understand correctly, you want index i
of counts
to hold the number of times that the dice summed to that index. To do that:
int sumOfDice = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1);
counts[sumOfDice]++;
To return an array:
Returning an array is much like returning an int. Just change the method declaration for 'input()' to
public static int[] input () throws IOException {
and the return statement to
return counts;
To print an array:
Import java.util.Arrays
and call Arrays.toString(yourArrayHere)
.
The complete program now looks like:
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;
}
}
(by user2184171、Achintya Jha、raptortech97)