简单的Java游戏遇到麻烦

smj7v3

我正在为一个班级编写程序,其中玩家和计算机正在玩游戏。游戏包括挑选1、2、3或4个火柴棍。玩家从21输中挑出最后一根木棍。操纵游戏,使计算机始终获胜。

无论如何,我的问题在于结束游戏的逻辑。这是我的代码:

 public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    String player;
    int matchstick;
    int total = 0;

    System.out.println("Rules of the game: \nThe player and computer take turns picking 1, 2, 3 , or 4 matchsticks."
                        + " \nThe player to pick the last stick out of 21 loses.");
    System.out.print("\nHello player. Please enter your name: ");
    player = keyboard.next();

     while (total < 21) {

        System.out.print("\nHow many matchsticks do you pick, " + player + "? ");
        matchstick = keyboard.nextInt();

        while (matchstick > 4) {
            System.out.print("You have exceeded the limit of 4 matchsticks. Please try again. \n\nHow many matchsticks do you pick? ");
            matchstick = keyboard.nextInt();
        }

        total += matchstick;

        System.out.println(player + " has picked " + matchstick + " matchstick(s) and brings the total to " + total + " matchsticks.");

        if (total == 21) {
            System.out.println("You picked the last matchstick... YOU LOSE!!!");
        }

        System.out.println("\nNow it's the computer's turn.");

        if (matchstick == 1) {
            total += 4;
            System.out.println("The computer chooses 4 matchsticks, bringing the total to " + total + " matchsticks.");
        }
        if (matchstick == 2) {
            total += 3;
            System.out.println("The computer chooses 3 matchsticks, bringing the total to " + total + " matchsticks.");
        }
        if (matchstick == 3) {
            total += 2;
            System.out.println("The computer chooses 2 matchsticks, bringing the total to " + total + " matchsticks.");
        }
        if (matchstick == 4) {
            total += 1;
            System.out.println("The computer chooses 1 matchstick, bringing the total to " + total + " matchsticks.");
        }

    }

}

}

当while循环的条件(总计<21)为false时,我该如何跳过System.out.println(“您选择了最后一根火柴棒...您输了!!!”)之后的行?满足条件后,有没有办法打破中间循环?还是我的逻辑完全错误?

阿拉伯人

你可以只return从方法main

  ...
if (total == 21) {
     System.out.println("You picked the last matchstick... YOU LOSE!!!");
     return;
}
  ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

简单的Java纸牌游戏-麻烦改组游戏(数组)

来自分类Dev

Java-在基于图块的游戏中创建洞穴时遇到麻烦

来自分类Dev

使用简单的awk脚本遇到麻烦

来自分类Dev

使用Java的FileChooser遇到麻烦

来自分类Dev

麻烦制作Java轮盘游戏

来自分类Dev

在游戏中制作大量咒语时遇到麻烦

来自分类Dev

c语言猜数字游戏遇到了麻烦

来自分类Dev

简单的java乘法游戏

来自分类Dev

C / C ++编译。在项目的简单Makefile上遇到麻烦

来自分类Dev

使用Javascript添加系统时遇到麻烦(简单,但速度很慢)

来自分类Dev

遇到什么应该是简单的bash脚本的麻烦

来自分类Dev

运行简单的空白行删除时遇到麻烦

来自分类Dev

在Java中遇到ActionEvent和getText()的麻烦

来自分类Dev

学习Java构造函数时遇到麻烦

来自分类Dev

跟踪Java的递归方法时遇到麻烦

来自分类Dev

简单的Java / Swing GUI游戏

来自分类Dev

我在制作剪刀石头布游戏时遇到了麻烦(3个版本中最好的)

来自分类Dev

Pygame-在小行星游戏中创建多个小行星对象时遇到麻烦

来自分类Dev

游戏:九个正面和反面。我在处理二进制文件时遇到麻烦

来自分类Dev

我是编程新手,在制作带有BlackSack游戏的套装卡片时遇到麻烦

来自分类Dev

在JAVA中遇到线程和信号量的麻烦

来自分类Dev

在大学进行Java练习时遇到麻烦(字符串)

来自分类Dev

在Java中使用递归方法时遇到麻烦

来自分类Dev

在Win7机器上卸载Java时遇到麻烦

来自分类Dev

在Java中链接方法调用时遇到麻烦

来自分类Dev

Java在找出Quicksort中位数3时遇到麻烦

来自分类Dev

使用HTML5在React中构建简单的音频播放器时遇到麻烦

来自分类Dev

用python创建一个简单的IRC机器人。遇到麻烦

来自分类Dev

在主调用方法时遇到麻烦?简单的客户端/服务器

Related 相关文章

  1. 1

    简单的Java纸牌游戏-麻烦改组游戏(数组)

  2. 2

    Java-在基于图块的游戏中创建洞穴时遇到麻烦

  3. 3

    使用简单的awk脚本遇到麻烦

  4. 4

    使用Java的FileChooser遇到麻烦

  5. 5

    麻烦制作Java轮盘游戏

  6. 6

    在游戏中制作大量咒语时遇到麻烦

  7. 7

    c语言猜数字游戏遇到了麻烦

  8. 8

    简单的java乘法游戏

  9. 9

    C / C ++编译。在项目的简单Makefile上遇到麻烦

  10. 10

    使用Javascript添加系统时遇到麻烦(简单,但速度很慢)

  11. 11

    遇到什么应该是简单的bash脚本的麻烦

  12. 12

    运行简单的空白行删除时遇到麻烦

  13. 13

    在Java中遇到ActionEvent和getText()的麻烦

  14. 14

    学习Java构造函数时遇到麻烦

  15. 15

    跟踪Java的递归方法时遇到麻烦

  16. 16

    简单的Java / Swing GUI游戏

  17. 17

    我在制作剪刀石头布游戏时遇到了麻烦(3个版本中最好的)

  18. 18

    Pygame-在小行星游戏中创建多个小行星对象时遇到麻烦

  19. 19

    游戏:九个正面和反面。我在处理二进制文件时遇到麻烦

  20. 20

    我是编程新手,在制作带有BlackSack游戏的套装卡片时遇到麻烦

  21. 21

    在JAVA中遇到线程和信号量的麻烦

  22. 22

    在大学进行Java练习时遇到麻烦(字符串)

  23. 23

    在Java中使用递归方法时遇到麻烦

  24. 24

    在Win7机器上卸载Java时遇到麻烦

  25. 25

    在Java中链接方法调用时遇到麻烦

  26. 26

    Java在找出Quicksort中位数3时遇到麻烦

  27. 27

    使用HTML5在React中构建简单的音频播放器时遇到麻烦

  28. 28

    用python创建一个简单的IRC机器人。遇到麻烦

  29. 29

    在主调用方法时遇到麻烦?简单的客户端/服务器

热门标签

归档