Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:27:05+00:00 2026-06-12T23:27:05+00:00

I’m new to Java, I’ve been learning for about a month. One of the

  • 0

I’m new to Java, I’ve been learning for about a month. 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. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:27:06+00:00Added an answer on June 12, 2026 at 11:27 pm

    The problem you have that all of your conditions return false and you do not have any else clause.

    The reason why they are false is that you perfrom invalid compare over String type.

    so instead having

    horse == "a", you should have "a".equals(horse).

    Or you can switch to primitive type char then horse == 'a' will be correct.

    In Java operator == compare the reference for Object type and value for primitive.

    So every ware you are using Object types you should keep in mind to use equals method instead of ==

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.