Craps Game JAVA - 忽略输入验证?

时间:2015-10-24 07:55:04

标签: java validation class input do-while

这是我在这里问的第一个问题。我目前在CSCI 2911进行实验,我们必须编写一个基本的Craps游戏。一切都在运作,直到我添加了一个简单的投注系统。一旦我实现它,它就不起作用。

以下是我的java类的代码:

主要课程:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rjohncraps;
import java.util.Scanner;
/**
 *
 * @author Randall
 */
public class RJohnCraps {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);

        // Game Object
        Game g = new Game ();
        // Win Initalization
        int wins = 0;
        // Money Initlization
        int money = 0;
        // Bet Amount Initialization
        int bet = 0;
        // GameInput Initialization
        String gameInput = " ";

        System.out.println("Want to play a game of Craps? (y/n)");
        gameInput = input.nextLine();

        // If gameInput is yes or no
        switch (gameInput) {
            case "Y":
            case "y":
            case "Yes":
            case "yes": //If yes.

                // Game do-while
                do {
                    do { // Keep asking until bet is more or equal to zero
                        System.out.println("How much do you want to bet?");
                        bet = input.nextInt();
                    } while (bet <= 0);

                    // return character in play() [Game.java] to char result
                    char result = g.play();

                    switch (result) {
                        case 'W': // If Wins
                            System.out.println("You won!");
                            wins = wins + 1; // Add One Win to Win Count
                            money = money + bet; // Add Bet Amount to Total Money Amount
                            break;
                        case 'L': // If Loses
                            System.out.println("You lose!");
                            money = money - bet; // Deduct Bet Amount to Total Money Amount
                            break;
                    }

                    System.out.println("Do you want to play again? (y/n)"); // Ask to play again

                    gameInput = input.nextLine();  

                // If gameInput = yes, then it will repeat
                } while (gameInput.equals("y") || gameInput.equals("Y"));

                // If gameInput = no
                System.out.println("Your total wins is "+wins+" wins.");

                // It total money is less than zero.
                if (money < 0) {
                    // If it's zero, then it's a negative, so multiply money by -1 to be positive
                    money = money * -1;
                    System.out.println("You owe "+money+" dollars.");

                } else if (money > 0) { // If total money is more than zero
                    System.out.println("You earned "+money+" dollars.");

                } else { // If total money is zero.
                    System.out.println("You earned nothing.");
                }

                break;

            // If Don't want to play    
            case "N":
            case "n":
            case "No":
            case "no":

                // Print a nice message
                System.out.println("Okay, have a nice day!");
                break;
            // If Default, Print Invalid Message                
            default:
                System.out.println("Invalid Word");

        }

    }

}

游戏类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rjohncraps;

/**
 *
 * @author Randall
 */
public class Game {
    // Int Variables (set to private)
    private int result;
    private int point;

    // Craps Play
    public char play() {
        // Dice Object from [Dice class]
        Dice d = new Dice();
        // Blank Char
        char blank = ' ';

        // return int from rollTwoDice() to int result
        result = d.rollTwoDice();

        // Print the results from the Dice Rolls
        System.out.print(result+", ");

        // If First Roll is 2,3,12
        if (result == 2 || result == 3 || result == 12) {
            // Return L char
            return 'L';
        }

        // If First Roll 7 and 11
        else if (result == 7 || result ==11) {
            // Return W char
            return 'W';

        // If neither 2,3,12,7,11
        } else {
            // Assigned Result to Point variable
            point = result;

            // Loop until Result equals 7 or Point (First Roll)
            do {
                // return integer from rollTwoDice() to result variable
                result = d.rollTwoDice();
                // print result
                System.out.print(result+", ");
            } while (result != 7 && result != point);

            // if result equals 7
            if (result == 7) {
                // Return char L
                return 'L';
            }

            // if result equals point (first roll)
            if (result == point) {
                // return W char
                return 'W';
            }

        }

        return blank;

    }
}

骰子类(由于我不能发布超过2个链接,我会在这里发布Dice类的代码,因为它是最短的。)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rjohncraps;
import java.util.Random;
/**
 *
 * @author Randall
 */
public class Dice {
    // Int Variable for dice one, dice two, and sum of d1 and d2
    private int d1;
    private int d2;
    private int sum;

    // Random object
    Random bag = new Random();

    // Rolling Dice Method
    public int rollTwoDice() {
        // Sum of Two Dice Initialization
        int sum = 0;

        // Generate random numbers for dice one and dice two
        d1 = bag.nextInt(6)+1;
        d2 = bag.nextInt(6)+1;

        // Calculate the sum of dice 1 and dice 2
        sum = d1 + d2;

        // Return sum
        return sum;
    }
}

以下是(问题):

当我运行代码(来自主类)时, 首先它会询问我是否要玩Craps,我将在其中键入&#34; y&#34;从我的键盘。

然后它会询问我想要多少赌注,我在其中输入一个积极的整数。例如,100。

之后它将打印骰子卷的结果,我得到8,9,8。如果你知道Craps游戏,那意味着我赢了。

它想说&#34;你想再玩一次吗? (Y / N),&#34;我想在其中键入&#34; y&#34;或&#34; n&#34;,但它会忽略键盘输入部分,它只会打印我获得的总胜利和我赚取的金额。

这是输出的样子:

Want to play a game of Craps? (y/n)
y
How much do you want to bet?
100
8, 9, 8, You won!
Do you want to play again? (y/n)
// I suppose to type something in the keyboard, 
// but it skips that and just prints the results.
Your total wins is 1 wins.
You earned 100 dollars.
BUILD SUCCESSFUL (total time: 1 minute 14 seconds)

我不知道发生了什么事情,看起来好像被贴上了标签(评论&#34; Game Do-While&#34;似乎无法发挥作用。你们觉得呢?我不知道。

如果您需要更多信息,请告诉我们!

0 个答案:

没有答案