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))


問題描述

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 user2184171Achintya Jharaptortech97)

參考文件

  1. Type Mismatch can't convert from Boolean to Int (Arrays in custom method) (CC BY‑SA 3.0/4.0)

#java #return #arrays






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論