0

i have a slot machine program. the program works properly but what doesnt is the asking if they would like to countinue playing. the specific line of code is "

    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 you like to play the slot machine?");
    ans= scan.nextLine();

    if(ans.equalsIgnoreCase("yes")){
    System.out.println("How much moeny do you 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 much would u like to bet?");
    bet = scan.nextInt();        

    while(bet>money)
    {

        System.out.println("you do not have enough money");
        System.out.println("How much would u like to bet?");
        bet = scan.nextInt();

    }
    hold = money-bet;
    hold1=hold;

    if(slota.equals(slotb)  && slotb.equals(slotc))
    {
        System.out.println("congrats you 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(" you lost");
        System.out.println("you have $"+hold1);  
        System.out.println("" +slota +" " +slotb +" " +slotc);            
    }        

    if(slota.equals(slotb) && !slota.equals(slotc))
    {
        System.out.println("keep your 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 your 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 your 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 are broke");
        ans=("no");

    }
    money=hold1;
        }
    System.out.println("play again?");
    ans =scan.nextLine();
    }     
}
}
Shriram
  • 4,124
  • 7
  • 28
  • 58
Bhagyesh
  • 576
  • 6
  • 21

2 Answers2

0

Try this:

scan.nextLine();
ans = scan.nextLine();

There is a newline character sitting out there that you need to eat up.

brso05
  • 12,634
  • 1
  • 17
  • 37
0

I posted this elsewhere too but...

Try using scnr.nextLine(); instead of scnr.next();

The reason for this is because of tokens. next()'s documentation says:

public String next()

Finds and returns the next complete token from this scanner.

Token generally is separated by whitespaces ("\n", "\t", " ") and therefore will not recognize your "enter" or "\n" character as a token. Which is why it'll keep reading, thinking that you haven't entered any tokens.

nextLine() on the other hand will read until it finds a "\n" character. That means when you enter, it's reading in a "\n" character, thereby setting your choice to be "".

Community
  • 1
  • 1
sparkhee93
  • 1,343
  • 3
  • 19
  • 27