Java Dice Game-有关“目标”号码的问题

时间:2018-11-09 19:43:26

标签: java

我正在为骰子游戏编写代码,其中某些数字允许您获胜,某些数字允许您输钱,然后另一组数字将成为您需要实现的目标数字。例如,如果您投出9,则获胜;如果投出12,则输。但是,如果您掷出10,则该数字成为目标,如果您再次掷出该数字,您将获胜。但是,如果掷13,就会输。我正在努力理解如何使最后一部分发挥作用,如何保持目标数以及使用户成败。

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

public class DiceTestZ{

    static int roll()
    {
        int die1 = (int)(Math.random()*6) + 1;

        return die1;
    }


    static boolean playerWins(int dieSum)// I then created a method that would return a boolean if the sum of two of my random numbers were equal to the winning numbers.
{
       return dieSum == 9 || dieSum == 11 || dieSum == 18 || dieSum == 24;
}


    static boolean playerLoses(int dieSum)// This method is the exact same as the one above it, but this method is true when the sum of the dice equals the losing numbers.
{
        return dieSum == 6 || dieSum == 12 || dieSum == 13 || dieSum == 17 || dieSum == 19 || dieSum == 23; 
}

    static boolean playerSets(int dieSum)// This method is the exact same as the ones above it, but this method is true when the sum of the dice equals any of the goal numbers.
{
        return dieSum == 22 || dieSum == 21 || dieSum == 20 || dieSum == 16 || dieSum == 15 || dieSum == 14 || dieSum == 10 || dieSum == 8 || dieSum == 7 || dieSum == 5 || dieSum == 4; 
}

public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    System.out.println("Welcome to the CocoPanther Dice Game!");
    System.out.println("Created by Noah Ilovar.");
    System.out.println("In this game we will roll 4 dice. If you roll a 9,11,18 or 24 you win. But if you roll a 6, 12, 13, 17, 19 or 23 you lose.");
    System.out.println("If you roll any other number that number becomes the new goal and if it is rolled you win. But if you roll an 13, you lose");

    System.out.println("What is your name?");
    String inNext =in.nextLine();
    System.out.println("Welcome,"+inNext);


    while(true)
    {
        String again = "y";
        int die1 = roll();
        int die2 = roll();
        int die3 = roll();
        int die4 = roll();

        Scanner keyboard = new Scanner(System.in);

        int dieSum = die1 + die2 + die3 + die4;

        System.out.println("You've rolled a " +dieSum);

        if(playerWins(dieSum))
        {
            System.out.print("Congratulations, you've won!");

        }

        if(playerLoses(dieSum))
        {
            System.out.println("Sorry, you've lost the game.");

        }

        if(playerSets(dieSum))
        {
            System.out.println("You've rolled a goal number! Roll it again to win.");

        }

            System.out.println(" Roll them again (y=yes)? ");
            again = keyboard.nextLine();

        }
    }
}

0 个答案:

没有答案