我如何询问用户使用netbeans在Java中每次掷骰子后是否要继续游戏? [英] How do I ask if the user would like to continue the game after each dice roll in java using netbeans?

查看:85
本文介绍了我如何询问用户使用netbeans在Java中每次掷骰子后是否要继续游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关此问题的帮助:

I need help with this question:

掷骰子游戏有两个六面骰子.玩游戏的用户将掷出两个骰子,并且将生成介于1和6之间的两个随机数.这两个数字的总和将用于决定下一步.

A dice rolling game is played with two six-sided dice. A user playing the game, will roll the two dice and two random numbers between one and six will be generated. The sum of the two numbers will be used to decide the next step.

如果总和为2,3或12,则玩家获胜.如果总和是7或11,则他/她输了.如果总和为4、5、6、8、9或10,则程序会自动再次掷骰子,直到玩家赢或输.

If the sum is 2,3 or 12 then the player wins. If the sum is 7 or 11 then he/she loses. If the sum is 4, 5, 6, 8, 9 or 10 then the program automatically rolls the dice again until the player wins or loses.

每次掷骰子后,都会提示玩家输入.玩家应该决定游戏是否应该继续.

After every dice roll, the player will be prompted for an input. The player should decide if the game should continue or not.

每次掷骰子后,也应该显示获胜和输掉的游戏数量.

The amount of games won and lost should also be displayed after every dice roll.

我设法使第一部分开始工作,但是无法弄清楚如何提示用户是否希望继续或他们赢了/输了多少游戏

I have managed to get the first part to work but unable to work out how to prompt the user if they wish to continue or how many games they have won/lost

public class DiceGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      
        while (true) {
            int dice1=(int)(Math.random()*6+1);
            int dice2=(int)(Math.random()*6+1);
            int sum = dice1 + dice2;
            
            System.out.println("Roll: total = " + sum);
            
            if (sum==2 || sum==3 || sum==12) {
                System.out.println("Sorry with a " +  sum  + " you loose :(");
                break;
            }
            else if(sum==7 || sum==11) {
                System.out.println("With a " + sum + " you win :)");
                break;
            }
        }
    }
}

推荐答案

扫描仪可用于提示用户询问他/她是否要继续.

Scanner can be used to prompt user to ask whether he/she wants to continue or not.

您甚至可以跟踪编号.掷骰子的次数.

You can even track no. of times dices were rolled.

import java.util.Scanner;

public class DiceGame {

    public static int attempt = 1;
    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int dice1 = (int) (Math.random() * 6 + 1);
        int dice2 = (int) (Math.random() * 6 + 1);
        int sum = dice1 + dice2;

        while (true) {
            System.out.println();
            System.out.println("Rolling dice for " + attempt + " time!");

            dice1 = (int) (Math.random() * 6 + 1);
            dice2 = (int) (Math.random() * 6 + 1);
            sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum == 2 || sum == 3 || sum == 12) {
                System.out.println("Sorry with a " + sum + " you loose :(!");
                System.out.println();
                break;
            } else if (sum == 7 || sum == 11) {
                System.out.println("With a " + sum + " you win :)!");
                System.out.println();
                break;
            }
            System.out.println();
            System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT");
            if (!scanner.next().equalsIgnoreCase("y")) {
                break;
            }
            attempt++;
        }
        System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
    }
}

如果要在总和为4 5 6 8 9 10时自动掷骰子,则不再需要Scanner,即用户输入是否继续.

If you want to roll the dices automatically when the sum is 4 5 6 8 9 10 then you would no more need Scanner i.e. user input whether to continue or not.

这是相同的解决方案.

public class DiceGame {

    public static int attempt = 1;

    public static void main(String[] args) {

        int dice1 = 0;
        int dice2 = 0;
        int sum = 0;

        while (true) {
            System.out.println();
            System.out.println("Rolling dice for " + attempt + " time!");

            dice1 = (int) (Math.random() * 6 + 1);
            dice2 = (int) (Math.random() * 6 + 1);
            sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum == 2 || sum == 3 || sum == 12) {
                System.out.println("Sorry with a " + sum + " you loose :(!");
                System.out.println();
                break;
            } else if (sum == 7 || sum == 11) {
                System.out.println("With a " + sum + " you win :)!");
                System.out.println();
                break;
                // this will roll the dices automatically
                // when sum is 4, 5, 6, 8, 9 or 10
            } else {
                System.out.println();
                System.out.println("With " + sum + " dices are rolled again automatically!!");
                attempt++;
            }
        }
        System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
    }
}

样品运行

Rolling dice for 1 time!
Roll: total = 4

With a 4, dices are rolled again automatically!!

Rolling dice for 2 time!
Roll: total = 6

With a 6, dices are rolled again automatically!!

Rolling dice for 3 time!
Roll: total = 2
Sorry with a 2 you loose :(!

Thanks for playing dice game, you rolled the dice 3 times!

这篇关于我如何询问用户使用netbeans在Java中每次掷骰子后是否要继续游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆