gpt4 book ai didi

java - 测试时老虎机问题

转载 作者:行者123 更新时间:2023-11-30 06:05:26 24 4
gpt4 key购买 nike

我的老虎机程序的输出中发现一些不太正确的内容。这是我得到的输出:

Want to test your luck?
To Spin, type any positive number
To End Game, type -1 and press enter
2
21
0
We have twins... You've matched a pair!
Want to test your luck?
To Spin, type any positive number
To End Game, type -1 and press enter

前三行是您首次运行该程序时显示的内容。在本例中,我输入数字 2 开始。

  • 我遇到的一个问题是开始游戏的输入被计为插槽的数字之一。

  • 我遇到的第二个问题是,在每个插槽的 3 个数字中,我得到的数字大于 9,当我指定为 0-9 之间时,我不明白这是怎么回事。

  • 最后但并非最不重要的一个错误是,我在代码中遇到的最后一个错误是,我设置的数字后面的一行显示“我们有双胞胎……您匹配了一对!”虽然没有一对?除非只是计算两个。

这是我的完整程序:

import java.util.Scanner;
import java.util.Random;

public class Java_Lab_3 {
private static int endgame = 0;

public static void main(String[] args) {

int slot1;
int slot2;
int slot3;
int count1 = 0;
Random rand = new Random();
Scanner keyboard = new Scanner(System.in);

while (endgame != -1) {
System.out.println("Want to test your luck?");
System.out.println("To Spin, type any positive number");
System.out.println("To End Game, type -1 and press enter ");

endgame = keyboard.nextInt();

slot1 = rand.nextInt(10);
slot2 = rand.nextInt(10);
slot3 = rand.nextInt(10);

System.out.println(slot1 + slot2 + slot3);
System.out.println(count1);

if (slot1 == slot2 && slot1 == slot3) { // Then they have JACKPOT
System.out.println("You've Won!");
} else if (slot1 == slot2 || slot2 == slot3 || slot1 == slot3) { //
They MATCHED 2
System.out.println("We have twins... You've matched a pair!");
} else {
System.out.println("Sucks to Suck, don't quit your day job!");

}
}
}
}

最佳答案

你的程序运行良好。只需更改

System.out.println(slot1 + slot2 + slot3);

System.out.println(slot1 + " " + slot2 + " " + slot3);

执行字符串连接而不是加法,这样您就可以看到插槽的实际值而不是它们的总和。

关于java - 测试时老虎机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433881/

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