简单的Java赛车游戏

时间:2012-10-17 12:41:17

标签: java simulator currency gambling

我是Java的新手,我已经学习了大约一个月。课堂上的其中一个项目就是写一个你打赌“马”“比赛”的程序。这是代码:

import java.util.Scanner;
import java.util.Random;
public class horsies {
    public static void main (String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int money = 1000; //Set original $ to 1000
        int r; //Declare variable for random number of horse to proceed
        int races = 0; //Set total races to 0
        int garfwins = 0; //Set Garfield's score to 0
        int shaunwins = 0; //Set Shaun's score to 0
        int chestwins = 0; //Set Chester's score to 0
        int garf; //Declare Garfield's progress variable
        int shaun; //Declare Shaun's progress variable
        int chest; //Declare Chester's progress variable
        String response; //Declare variable to get input on continuing game
        String horse; //Declare variable to get input on horse
        String track = "------------";
        String trackgarf;
        String trackshaun;
        String trackchest;
        int bet = 0;
        do {
            garf = 0;
            shaun = 0;
            chest = 0;
            System.out.print ("You have $"+money+"\n");
            System.out.print ("Hi, which horse would you like to bet on?\n");
            System.out.print ("a. Garfield ("+garfwins+"/"+races+")\n");
            System.out.print ("b. Shaun ("+shaunwins+"/"+races+")\n");
            System.out.print ("c. Chester ("+chestwins+"/"+races+")\n");        
            horse = input.next();
            System.out.print ("How much do you want to bet?\n");
            bet = input.nextInt();
            if (bet <= 0) {
                System.out.print ("Invalid bet.\n");
            }
            else {
                while (garf<12 && shaun<12 && chest<12){
                    r = (int) (Math.random()*3+1);
                    if (r == 1) {
                        garf++;
                    } else if (r == 2) {
                        shaun++;
                    } else if (r == 3) {
                        chest++;
                    }
                    System.out.print ("\n\n\n\n\n\n\n\n\n\n\n\n");
                    trackgarf = track.substring(0, garf)+"1"; //Get Garf's progress on track
                    trackshaun = track.substring(0, shaun)+"1"; //Get Shaun's progress on track
                    trackchest = track.substring(0, chest)+"1"; //Get Chester's progress on track
                    System.out.print (trackgarf+"\n");
                    System.out.print (trackshaun+"\n");
                    System.out.print (trackchest+"\n");
                    System.out.print ("GAR:"+garf+"\nSHA:"+shaun+"\nCHE:"+chest+"\n");
                    try {
                          Thread.sleep(1000L);
                            }
                        catch (Exception j) {}
                }
            }
            if (garf == 12 && horse == "a") {
                System.out.print ("You earned $"+(2*bet));
                money = money + (2 * bet);
                System.out.print ("Total balance: $"+money);
            } else if (shaun == 12 && horse == "b") {
                System.out.print ("You earned $"+(2*bet));
                money = money + (2 * bet);
                System.out.print ("Total balance: $"+money);
            } else if (chest == 12 && horse == "c") {
                System.out.print ("You earned $"+(2*bet));
                money = money + (2 * bet);
                System.out.print ("Total balance: $"+money);
            }
            System.out.print ("Play again?\n");
            response = input.next();

        } while (money >= 0 && (response.equals("Yes")||response.equals("yes")));
        input.close();
        }
}

该计划似乎运作良好,除了货币价值似乎仍然停留在1000的事实。任何建议都将受到赞赏。谢谢!

4 个答案:

答案 0 :(得分:2)

您需要使用equals()函数而不是== operator。

例如

if (garf == 12 && horse == "a") { 

应该是

if (garf == 12 && horse.equals("a")) { 

答案 1 :(得分:2)

您遇到的问题是您的所有条件都返回false并且您没有任何else子句。

它们之所以错误的原因是你对String类型进行了无效的比较。

所以改为

horse == "a",您应该"a".equals(horse)

或者您可以切换到基本类型char,然后horse == 'a'将是正确的。

在Java运算符==中比较对象类型的引用和基元的值。

因此,您使用对象类型的每件商品都应该记住使用equals方法而不是==

答案 2 :(得分:1)

你不从钱变量中减去任何东西。你应该在下注时这样做

答案 3 :(得分:0)

当您的马输了时,您的代码不会对钱进行任何更改,因此余额仍为1000

else       {
            System.out.print ("You lost $"+(bet)+" bucks in this race\n");
            money = money - (bet);
            } System.out.print ("Play again?\n Balance :"+money);

在循环中添加else语句