java - program wont wait for the proper input -
this question has answer here:
i have slot machine program. program works doesnt asking if countinue playing. specific line of code "
system.out.println("play again?"); ans =scan.nextline();"
package project3_16; import java.util.*; public class project3_16 { public static void main(string[] args) { scanner scan = new scanner(system.in); int money=0,bet=0,returnbet,hold=0,hold1=0,hold2=0; string ans=("yes"),slota,slotb,slotc; int again=0; string[] slot1 = {"۞","☯","☠","♕","∎","☢","♨","♣","Ω","★"}; while(ans.equalsignorecase("yes")){ system.out.println("would play slot machine?"); ans= scan.nextline(); if(ans.equalsignorecase("yes")){ system.out.println("how moeny have?"); money =scan.nextint(); } while(ans.equalsignorecase("yes")) { slota =(slot1[new random().nextint(slot1.length)]); slotb =(slot1[new random().nextint(slot1.length)]); slotc =(slot1[new random().nextint(slot1.length)]); system.out.println("how u bet?"); bet = scan.nextint(); while(bet>money) { system.out.println("you not have enough money"); system.out.println("how u bet?"); bet = scan.nextint(); } hold = money-bet; hold1=hold; if(slota.equals(slotb) && slotb.equals(slotc)) { system.out.println("congrats won"); hold= money+(bet*2); system.out.println("you have $"+hold1); system.out.println("" +slota +" " +slotb +" " +slotc); } if(!slota.equals(slotb) && !slotb.equals(slotc) && !slota.equals(slotc)) { system.out.println(" lost"); system.out.println("you have $"+hold1); system.out.println("" +slota +" " +slotb +" " +slotc); } if(slota.equals(slotb) && !slota.equals(slotc)) { system.out.println("keep money"); hold1=hold1+bet; system.out.println("you have $"+hold1); system.out.println("" +slota +" " +slotb +" " +slotc); } if(slotb.equals(slotc) && !slota.equals(slotc)) { system.out.println("keep money"); hold1=hold1+bet; system.out.println("you have $"+hold1); system.out.println("" +slota +" " +slotb +" " +slotc); } if(slotc.equals(slota) && !slotc.equals(slotb)) { system.out.println("keep money"); hold1=hold1+bet; system.out.println("you have $"+hold1); system.out.println("" +slota +" " +slotb +" " +slotc); } if(hold1==0 || money==0 || hold==0 ) { system.out.println("you broke"); ans=("no"); } money=hold1; } system.out.println("play again?"); ans =scan.nextline(); } } }
try this:
scan.nextline(); ans = scan.nextline();
there newline character sitting out there need eat up.
Comments
Post a Comment