【问题标题】:Type Mismatch can't convert from Boolean to Int (Arrays in custom method)类型不匹配无法从 Boolean 转换为 Int(自定义方法中的数组)
【发布时间】:2013-03-09 15:10:31
【问题描述】:

在我的程序中,应该创建 13 个数组,以保存 2-12 范围内的总和,在这种情况下,两个骰子滚动 1000 次,并且应该打印出骰子相加的次数滚动“2”。但是,最初该程序可以工作,但后来意识到我需要在程序中使用数组,而不仅仅是使用单个 math.random 方法。我已经尝试过,在 main 方法中简单地打印数组值,但出现更多错误。另外,我研究了使用 histogram 来调用主数组,除了我之前的尝试,它产生了更多的错误

我的问题是;

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

【问题讨论】:

    标签: java arrays return


    【解决方案1】:

    从布尔转换为整数:

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

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

    这使用ternary operator

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

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

    【讨论】:

    • 在行 int[] counts = input();我得到错误不兼容的类型
    • @user2184171 哎呀。我忘了更改方法声明。已编辑。
    • 如果我添加了一个十一面骰子,除了 do (int)(Math.random ()*11+1) 之外,我是否会粘贴相同的东西,并计算它掷出 2 的次数一个 11 面骰子?
    • @user2184171 听起来不错,但您应该检查一下您不希望使用 11 面骰子掷出 4 的次数(尽管另一方面,它们应该是相同的) .
    • 那会是 Count[2] 而不是 4?
    【解决方案2】:
    Sixside = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1) ;
    
    if(Sixside == 4)
       System.out.println("Print something");
    

    我假设您要检查条件并打印。

    counts[i] = Sixside;
    

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

    【讨论】:

    • 我在 random 语句之后复制了那个确切的代码,并在 Sixside = (int)(Math.random ()*6+1) 行收到错误“不兼容的类型,发现布尔需要 int” +(int)(Math.random ()*6+1) ;并且错误找不到符号变量 i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2021-05-31
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多