Virtual horse race   #


/** * Program : A virtual horse game, you pick your horse, and bet $100, if your horse comes first you win $500, else you loose your $100 bet. * Input : Choose your horse (a number beteen 1 and 6) * Output : Win or loose * Logic : Use random function to pick the winner horse out the total 6 horses. */ class Horseracing { // start of program public static void main(String[] args) { // no money int money = 0; // let the player choose the horse to bet on. System.out.println("Welcome to the Virtual Horse Races!"); System.out.println("There are 6 horses! 1, 2, 3, 4, 5, and 6! Bet on one of these!"); int horse = readLine(); // intro text, before race begins System.out.println("Well then! You think " + horse + " will win right? You will bet $100 on it!"); System.out.println("Let the races begin!"); // display the horse that got a good start int random = 0; random = (int )(Math.random() * 6 + 1); System.out.println(random + " is off to a good start!"); // loop through x times (e.g. 24) and show the leading horse. for(int j = 1; j <= 24; j++) { random = (int )(Math.random() * 6 + 1); int wait = (int )(Math.random() * 100 + 1); System.out.println(random + " is in the lead!"); try { Thread.sleep(wait); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // your horse came first. if(random == horse) { money = 500; System.out.println("You won the bet! You get $500!"); } else { money = 0; System.out.println("Aw...you lost...You have to give me $100!"); } System.out.println("Your current money is " + money); } // end of main() // read the input (horse number in this case) public static int readLine() { try { java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String line = stdin.readLine(); int foo = Integer.parseInt(line); return (foo); } catch (java.io.IOException e) { System.out.println(e); } catch (NumberFormatException e) { System.out.println(e); } return 0; } // end of readLine() } // end of class

Loading Please Wait...