简体   繁体   English

简单的Java赛车游戏

[英]Simple Java Racing Game

I'm new to Java, I've been learning for about a month. 我是Java新手,已经学习了大约一个月。 One of the projects in class is to write a program where you bet on "horse" "races." 课堂上的一个项目是编写一个程序,在此下注“马”,“比赛”。 Here's the code: 这是代码:

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();
        }
}

The program seems to work well, except for the fact that the money value seems to remain stuck at 1000. Any advice would be appreciated. 该程序似乎运行良好,除了货币价值似乎停留在1000的事实。任何建议,我们将不胜感激。 Thanks! 谢谢!

You need to use equals() function rather than == operator. 您需要使用equals()函数而不是==运算符。

For example 例如

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

should be 应该

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

The problem you have that all of your conditions return false and you do not have any else clause. 您遇到的所有条件都返回false并且没有任何else子句的问题。

The reason why they are false is that you perfrom invalid compare over String type. 它们为假的原因是您对String类型进行了无效的比较。

so instead having 所以有

horse == "a" , you should have "a".equals(horse) . horse == "a" ,你应该有"a".equals(horse)

Or you can switch to primitive type char then horse == 'a' will be correct. 或者,您可以切换到原始类型char然后horse == 'a'将是正确的。

In Java operator == compare the reference for Object type and value for primitive. 在Java运算符==比较对象类型的引用和基元的值。

So every ware you are using Object types you should keep in mind to use equals method instead of == 因此,您在使用对象类型的每种商品都应牢记使用equals方法而不是==

And you don't subtract anything from the money variable. 而且您不会从money变量中减去任何东西。 You should do it when you make the bet 您下注时应该这样做

When your horse loses, Your code does make any change in the money so balance remains 1000 当您的马输了时,您的代码确实对金钱进行了更改,因此余额保持为1000

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

Add a else statement to your loop 将else语句添加到循环中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM