【问题标题】:Simple Java Racing Game简单的 Java 赛车游戏
【发布时间】:2012-10-07 17:23:02
【问题描述】:

我是 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 的事实。任何建议将不胜感激。谢谢!

【问题讨论】:

  • 感谢您的回复! @cerealy 想通了,我忘了从钱中减去赌注。我写的是: System.out.print ("你想赌多少钱?\n");赌注 = input.nextInt(); if (bet

标签: java simulator currency gambling


【解决方案1】:

当你的马输了,你的代码确实会改变钱,所以余额保持在 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 语句

【讨论】:

    【解决方案2】:

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

    它们为假的原因是您对字符串类型进行了无效比较。

    所以有

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

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

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

    所以你使用的每一个对象类型你都应该记住使用equals方法而不是==

    【讨论】:

      【解决方案3】:

      而且你不会从货币变量中减去任何东西。下注时应该这样做

      【讨论】:

      • 完美!谢谢你,我不敢相信我忘了这样做。
      【解决方案4】:

      您需要使用 equals() 函数而不是 == 运算符。

      例如

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

      应该是

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

      【讨论】:

      • 实际上,即使在那之后,我也遇到了同样的问题。当你再次尝试玩时,它显示的钱仍然是 1000。
      • @Abu 它不是在 Java 中比较字符串的有效方法。
      • @Panah 如果对您有帮助,请您接受答案。谢谢。
      • @Panah 你是否在每次比较时将 == 更改为等于(即在 3 个 if 语句中)?
      • 感谢@Zyga,刚刚使用您的提示修复了接近末尾的代码,以显示剩余的钱。我还使用了 or 运算符“||”在 if 语句中减少代码的长度,并修复了支出为 1:1 的错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多