methods - fix java logical error, user money does not deduct or add whether it lose or win -
methods - fix java logical error, user money does not deduct or add whether it lose or win -
i want create java dice game utilize branching (if-else), nested while loops, , method (simple function , procedures).
the code run fine when comes result determine how much money user have, not show anything, show default money 100 how prepare it? give thanks much, beginner please not mine me question. here code:
public static void main(string[] args) { scanner in = new scanner(system.in); int umoney= 100; int lw; int roll1; int roll2; int bet ; system.out.println("you have "+ umoney+" dollars"); while (umoney!=0) { bet = getbet(in, umoney); char user=gethighlow(in); roll1=getroll(); system.out.println("die 1 rolls: "+roll1); roll2=getroll(); system.out.println("die 2 rolls: "+roll2); system.out.println("total of 2 diece is: " + (roll1+roll2)); lw = determinewinnings(user, bet, (roll1+roll2)); if (lw<0) { system.out.println("you lost!"); } else { system.out.println("you won "+ lw+" dollars!"); umoney=umoney+lw; } } } // given scanner , current maximum amount of money, prompt user // integer representing number of dollars want bet. // number must between 0 , maximum number of dollars. if user enters // number out of bounds, display error message , inquire again. // homecoming bet calling program. private static int getbet(scanner inscanner, int currentpool) { // fill in body system.out.println("enter amount bet (0 quit): "); int ubet= inscanner.nextint(); if (ubet==0) { system.out.println("goodbye!"); system.exit(0); } while (ubet<0 || ubet > currentpool) { system.out.println("you must come in between 0 , "+ currentpool +" dollars"); system.out.println("you have "+ currentpool+" dollars"); system.out.println("enter amount bet (0 quit): "); ubet=inscanner.nextint(); if (ubet==0) { system.out.println("goodbye!"); system.exit(0); } } homecoming ubet; } // given scanner, prompt user single character indicating whether // bet high ('h'), low ('l') or sevens ('s'). code should take // either capital or lower case answers, should display error if user attempts // come in 1 of these 3 values , prompt valid answer. // homecoming character calling program. private static char gethighlow(scanner inscanner) { // fill in body system.out.println("high, low or 7 (h/l/s): "); char iuword= inscanner.next().charat(0); while (iuword!='h' && iuword!='h' && iuword!='l' && iuword!='l' && iuword!='s' && iuword!='s') { system.out.println("invalid commad, please come in again!"); system.out.println("high, low or 7 (h/l/s): "); iuword= inscanner.next().charat(0); } homecoming iuword; } // produce random roll of single six-sided die , homecoming value calling // programme private static int getroll() { int roll1 =(int)(6*math.random())+1; homecoming roll1; } // given selection of high, low or sevens, player's bet , total result of // roll of dice, determine how much player has won. if player loses // bet winnings should negative. if player wins, winnings should // equal bet if selection high or low , 4 times bet if selection // sevens. homecoming winnings calling program. private static int determinewinnings(char highlow, int bet, int roll) { int result; if (highlow=='h' || highlow=='h') { if (roll<7) { result =-1*bet; } else { result=bet; } } if (highlow=='l'|| highlow=='l') { if (roll>7) { result=-1*bet; } else { result=bet; } } if (highlow=='s' || highlow=='s') { result =4*bet; } else { result = -1*bet; } homecoming result; }
there 2 problems code.
the lineumoney = umoney + lw
needs go after if-else block. want add together wins , losses, not wins :p you have missed 1 if status in determinewinnings - highlow == 's'
needs followed if (roll == 7)
java methods
Comments
Post a Comment