gpt4 book ai didi

java - 类型不匹配无法从 Boolean 转换为 Int(自定义方法中的数组)

转载 作者:行者123 更新时间:2023-11-30 04:27:20 25 4
gpt4 key购买 nike

在我的程序中,应该创建 13 个数组,以保存范围 2-12 的总和,在本例中,两个骰子滚动了 1000 次,并且应该打印出骰子添加到的次数滚动“2”。然而,最初该程序是有效的,但后来意识到我需要在程序中使用数组,而不是仅使用单个 math.random 方法。我尝试过,在主方法中简单地打印数组值,但会出现更多错误。另外,我研究了使用直方图将数组调用到主函数,除了我之前的尝试之外,它产生了严重的更多错误

我的问题是;

1:我如何修复主要错误,允许它从 boolean 转换为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];
}
}
}

最佳答案

从 boolean 值转换为整数:

一般来说,假设你有一些 boolean 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;
}
}

关于java - 类型不匹配无法从 Boolean 转换为 Int(自定义方法中的数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com