gpt4 book ai didi

java - 如何将多个 ArrayList 连接成一个?

转载 作者:行者123 更新时间:2023-12-01 10:25:16 25 4
gpt4 key购买 nike

我必须编写一段代码,将 10 个骰子掷“N”次。我必须使用“*”制作结果的直方图(10 到 60),以显示应该出现的钟形曲线。如何组合所有不同的 ArrayList 以使我的代码看起来更干净?有没有更简单的方法来同时完成这一切?

我得到了正确的结果;我只需要一种更干净或更有效的方式来显示我的代码。

public class TenDice {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.println("Enter number of trials: ");
int roll = scan.nextInt();


ArrayList<String> results10 = new ArrayList<>();
ArrayList<String> results11 = new ArrayList<>();
ArrayList<String> results12 = new ArrayList<>();
ArrayList<String> results13 = new ArrayList<>();
ArrayList<String> results14 = new ArrayList<>();
ArrayList<String> results15 = new ArrayList<>();
ArrayList<String> results16 = new ArrayList<>();
ArrayList<String> results17 = new ArrayList<>();
ArrayList<String> results18 = new ArrayList<>();
ArrayList<String> results19 = new ArrayList<>();
ArrayList<String> results20 = new ArrayList<>();
ArrayList<String> results21 = new ArrayList<>();
ArrayList<String> results22 = new ArrayList<>();
ArrayList<String> results23 = new ArrayList<>();
ArrayList<String> results24 = new ArrayList<>();
ArrayList<String> results25 = new ArrayList<>();
ArrayList<String> results26 = new ArrayList<>();
ArrayList<String> results27 = new ArrayList<>();
ArrayList<String> results28 = new ArrayList<>();
ArrayList<String> results29 = new ArrayList<>();
ArrayList<String> results30 = new ArrayList<>();
ArrayList<String> results31 = new ArrayList<>();
ArrayList<String> results32 = new ArrayList<>();
ArrayList<String> results33 = new ArrayList<>();
ArrayList<String> results34 = new ArrayList<>();
ArrayList<String> results35 = new ArrayList<>();
ArrayList<String> results36 = new ArrayList<>();
ArrayList<String> results37 = new ArrayList<>();
ArrayList<String> results38 = new ArrayList<>();
ArrayList<String> results39 = new ArrayList<>();
ArrayList<String> results40 = new ArrayList<>();
ArrayList<String> results41 = new ArrayList<>();
ArrayList<String> results42 = new ArrayList<>();
ArrayList<String> results43 = new ArrayList<>();
ArrayList<String> results44 = new ArrayList<>();
ArrayList<String> results45 = new ArrayList<>();
ArrayList<String> results46 = new ArrayList<>();
ArrayList<String> results47 = new ArrayList<>();
ArrayList<String> results48 = new ArrayList<>();
ArrayList<String> results49 = new ArrayList<>();
ArrayList<String> results50 = new ArrayList<>();
ArrayList<String> results51 = new ArrayList<>();
ArrayList<String> results52 = new ArrayList<>();
ArrayList<String> results53 = new ArrayList<>();
ArrayList<String> results54 = new ArrayList<>();
ArrayList<String> results55 = new ArrayList<>();
ArrayList<String> results56 = new ArrayList<>();
ArrayList<String> results57 = new ArrayList<>();
ArrayList<String> results58 = new ArrayList<>();
ArrayList<String> results59 = new ArrayList<>();
ArrayList<String> results60 = new ArrayList<>();

for (int i = 0; i < roll; i++) {

double dice1 = Math.random();
dice1 = (int) (dice1 * 6);

double dice2 = Math.random();
dice2 = (int) (dice2 * 6);

double dice3 = Math.random();
dice3 = (int) (dice3 * 6);

double dice4 = Math.random();
dice4 = (int) (dice4 * 6);

double dice5 = Math.random();
dice5 = (int) (dice5 * 6);

double dice6 = Math.random();
dice6 = (int) (dice6 * 6);

double dice7 = Math.random();
dice7 = (int) (dice7 * 6);

double dice8 = Math.random();
dice8 = (int) (dice8 * 6);

double dice9 = Math.random();
dice9 = (int) (dice9 * 6);

double dice10 = Math.random();
dice10 = (int) (dice10 * 6);

double total = dice1 + dice2 + dice3 + dice4 + dice5 +
dice6 + dice7 + dice8 + dice9 + dice10;

if (total == 10) {
results10.add("*");
}
if (total == 11) {
results11.add("*");
}
if (total == 12) {
results12.add("*");
}
if (total == 13) {
results13.add("*");
}
if (total == 14) {
results14.add("*");
}
if (total == 15) {
results15.add("*");
}
if (total == 16) {
results16.add("*");
}
if (total == 17) {
results17.add("*");
}
if (total == 18) {
results18.add("*");
}
if (total == 19) {
results19.add("*");
}
if (total == 20) {
results20.add("*");
}
if (total == 21) {
results21.add("*");
}
if (total == 22) {
results22.add("*");
}
if (total == 23) {
results23.add("*");
}
if (total == 24) {
results24.add("*");
}
if (total == 25) {
results25.add("*");
}
if (total == 26) {
results26.add("*");
}
if (total == 27) {
results27.add("*");
}
if (total == 28) {
results28.add("*");
}
if (total == 29) {
results29.add("*");
}
if (total == 30) {
results30.add("*");
}
if (total == 31) {
results31.add("*");
}
if (total == 32) {
results32.add("*");
}
if (total == 33) {
results33.add("*");
}
if (total == 34) {
results34.add("*");
}
if (total == 35) {
results35.add("*");
}
if (total == 36) {
results36.add("*");
}
if (total == 37) {
results37.add("*");
}
if (total == 38) {
results38.add("*");
}
if (total == 39) {
results39.add("*");
}
if (total == 40) {
results40.add("*");
}
if (total == 41) {
results41.add("*");
}
if (total == 42) {
results42.add("*");
}
if (total == 43) {
results43.add("*");
}
if (total == 44) {
results44.add("*");
}
if (total == 45) {
results45.add("*");
}
if (total == 46) {
results46.add("*");
}
if (total == 47) {
results47.add("*");
}
if (total == 48) {
results48.add("*");
}
if (total == 49) {
results49.add("*");
}
if (total == 50) {
results50.add("*");
}
if (total == 51) {
results51.add("*");
}
if (total == 52) {
results52.add("*");
}
if (total == 53) {
results53.add("*");
}
if (total == 54) {
results54.add("*");
}
if (total == 55) {
results55.add("*");
}
if (total == 56) {
results56.add("*");
}
if (total == 57) {
results57.add("*");
}
if (total == 58) {
results58.add("*");
}
if (total == 59) {
results59.add("*");
}
if (total == 60) {
results60.add("*");
}


}
System.out.println("10: " + results10 + "\n" + "11: " + results11 + "\n" + "12: " + results12 + "\n" + "13: " + results13 + "\n" +
"14: " + results14 + "\n" + "15: " + results15 + "\n" + "16: " + results16 + "\n" + "17: " + results17 + "\n" +
"18: " + results18 + "\n" + "19: " + results19 + "\n" + "20: " + results20 + "\n" + "21: " + results21 + "\n" +
"22: " + results22 + "\n" + "23: " + results23 + "\n" + "24: " + results24 + "\n" + "25: " + results25 + "\n" +
"26: " + results26 + "\n" + "27: " + results27 + "\n" + "28: " + results28 + "\n" + "29: " + results29 + "\n" +
"30: " + results30 + "\n" + "31: " + results31 + "\n" + "32: " + results32 + "\n" + "33: " + results33 + "\n" +
"34: " + results34 + "\n" + "35: " + results35 + "\n" + "36: " + results36 + "\n" + "37: " + results37 + "\n" +
"38: " + results38 + "\n" + "39: " + results39 + "\n" + "40: " + results40 + "\n" + "41: " + results41 + "\n" +
"42: " + results42 + "\n" + "43: " + results43 + "\n" + "44: " + results44 + "\n" + "45: " + results45 + "\n" +
"46: " + results46 + "\n" + "47: " + results47 + "\n" + "48: " + results48 + "\n" + "49: " + results49 + "\n" +
"50: " + results50 + "\n" + "51: " + results51 + "\n" + "52: " + results52 + "\n" + "53: " + results53 + "\n" +
"54: " + results54 + "\n" + "55: " + results55 + "\n" + "56: " + results56 + "\n" + "57: " + results57 + "\n" +
"58: " + results58 + "\n" + "59: " + results59 + "\n" + "60: " + results60);
}
}

最佳答案

是时候在 Java 8 中使用 Stream API 了。

要获得从 1 到 6 的 10 个骰子的总和,您可以这样做

Random rnd = new Random();

long sum = IntStream.range(0, 10).map(i -> rand.nextInt(6) + 1).sum();

您可以制作许多卷并获得这些卷的分配。

Map<Long, Long> rollCount = 
IntStream.range(0, roll)
.map(r -> rnd.ints(10, 1, 7).sum())
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));

因此,这将掷 10 个骰子,次并获得每个结果的计数。

您可以使用以下方式打印分布

rollCount.entrySet().stream()
.sorted(e -> e.getKey())
.forEach(e -> System.out.println(e + ": " +
IntStream.range(0, 10).mapToObj(i -> "*").collect(Collectors.joining("")));
<小时/>

假设我们不能使用 Java 8。我们可以使用循环和更简单的代码。

 List<String> countList = new ArrayList<>();
for(int i = 0; i <= 6 * 10; i++)
countList.add(""); // start with nothing.
Random rnd = new Random();
for (int r = 0; r < roll; r++) {
int sum = 0;
for (int i = 0; i < 10; i++)
sum += rnd.randInt(6) + 1;
String stars = countList.get(sum);
stars += "*";
countList.set(sum, stars);
}

关于java - 如何将多个 ArrayList 连接成一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384910/

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