java - 测试时老虎机问题

标签 java

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

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/

相关文章:

java - 是否有任何命令行函数可以从一个巨大的 Java 文件中返回主类的名称?

java - Servlet @WebServlet urlPatterns

java - 恢复操作栏

java - Apache HttpClient、User-Agent 和企业代理

java - Files.copy 的阻塞/同步版本

java - 使用 java8 过滤器比较两个整数列表

Java has-a 关系

java - onTouch 问题 - x < y 没有反应

java - java.lang.NoSuchMethodException 错误(布局)

java - 如何在 Selenium 3.6 中使用 IE11 执行 Shift 点击