d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Basic Java Assignment > Loops N What Not
12Next
Add Reply New Topic New Poll
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Nov 5 2014 03:30pm
So we have to make this slot machine esc program, input how much you bet, spin, and ask to repeat. It compiles fine but when I have the part that asks for a bet it like kills the choice to play again.

Here is my code now and the part with problem here is the part that when I remove it, it allows me to replay. So help on where I went wrong would be great. thanks.

Code
public class SlotMachine
{
public static void main(String[] args)
{
Random rand = new Random();
Scanner keyboard = new Scanner (System.in);
DecimalFormat cash = new DecimalFormat("#0.00");

double bet = 1, winnings;
int slot1, slot2, slot3;
String replay = "y";

do
{

System.out.print("How much would you like to bet?"); //problem
bet = keyboard.nextDouble(); //here

while(bet<0.01)
{
System.out.print("You must bet at least a penny, re enter a bet.");
bet = keyboard.nextDouble();
}


slot1 = rand.nextInt(5);

if (slot1 == 0)
System.out.print("Cherries ");

else if (slot1 == 1)
System.out.print("Oranges ");

else if (slot1 == 2)
System.out.print("Plums ");

else if (slot1 == 3)
System.out.print("Bells ");

else if (slot1 == 4)
System.out.print("Melons ");

else if (slot1 == 5)
System.out.print("Bars ");


slot2 = rand.nextInt(5);


if (slot2 == 0)
System.out.print("Cherries ");

else if (slot2 == 1)
System.out.print("Oranges ");

else if (slot2 == 2)
System.out.print("Plums ");

else if (slot2 == 3)
System.out.print("Bells ");

else if (slot2 == 4)
System.out.print("Melons ");

else if (slot2 == 5)
System.out.print("Bars ");


slot3 = rand.nextInt(5);


if (slot3 == 0)
System.out.println("Cherries ");

else if (slot3 == 1)
System.out.println("Oranges ");

else if (slot3 == 2)
System.out.println("Plums ");

else if (slot3 == 3)
System.out.println("Bells ");

else if (slot3 == 4)
System.out.println("Melons ");

else if (slot3 == 5)
System.out.println("Bars ");

if (slot1 == slot2 && slot1 == slot3)
{
winnings = bet * 3;
System.out.println("You won $" + winnings);
}


else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
{
winnings = bet * 2;
System.out.println("You won $" + winnings);
}


else
System.out.println("You won $0.");

System.out.print("roll again?");
replay = keyboard.nextLine();
}while(replay.equalsIgnoreCase("y"));

}
}


This post was edited by J-dawg on Nov 5 2014 03:55pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 5 2014 03:46pm
step 1: put code in code tags
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 5 2014 04:13pm
Is the problem you are having that the program won't replay when you read a double from the scanner?

If so, you probably need to do some debugging.

check to see what the value of "replay" is before the conditional at the end. Is it what you think it should be?

EX:

Code
System.out.print("roll again?");
replay = keyboard.nextLine();
System.out.println(replay); // verify the contents
}while(replay.equalsIgnoreCase("y"));


This post was edited by Eep on Nov 5 2014 04:16pm
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Nov 5 2014 04:26pm
Quote (Eep @ Nov 5 2014 04:13pm)
Is the problem you are having that the program won't replay when you read a double from the scanner?

If so, you probably need to do some debugging.

check to see what the value of "replay" is before the conditional at the end. Is it what you think it should be?

EX:

Code
System.out.print("roll again?");
    replay = keyboard.nextLine();
    System.out.println(replay);    // verify the contents
}while(replay.equalsIgnoreCase("y"));


Tried that and it just skipped over the section that where it should've printed out a 'y'
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 5 2014 05:46pm
God what ever IDE you're using formats your code as if it were autistic.

Code
public class SlotMachine
{
public static void main(String[] args) {
Random rand = new Random();
Scanner keyboard = new Scanner (System.in);
DecimalFormat cash = new DecimalFormat("#0.00");

double bet = 1, winnings;
int slot1, slot2, slot3;
String replay = "y";

do {

System.out.print("How much would you like to bet?"); //problem
bet = keyboard.nextDouble(); //here

while(bet<0.01) {
System.out.print("You must bet at least a penny, re enter a bet.");
bet = keyboard.nextDouble();
}


slot1 = rand.nextInt(5);

if (slot1 == 0)
System.out.print("Cherries ");

else if (slot1 == 1)
System.out.print("Oranges ");

else if (slot1 == 2)
System.out.print("Plums ");

else if (slot1 == 3)
System.out.print("Bells ");

else if (slot1 == 4)
System.out.print("Melons ");

else if (slot1 == 5)
System.out.print("Bars ");


slot2 = rand.nextInt(5);


if (slot2 == 0)
System.out.print("Cherries ");

else if (slot2 == 1)
System.out.print("Oranges ");

else if (slot2 == 2)
System.out.print("Plums ");

else if (slot2 == 3)
System.out.print("Bells ");

else if (slot2 == 4)
System.out.print("Melons ");

else if (slot2 == 5)
System.out.print("Bars ");


slot3 = rand.nextInt(5);


if (slot3 == 0)
System.out.println("Cherries ");

else if (slot3 == 1)
System.out.println("Oranges ");

else if (slot3 == 2)
System.out.println("Plums ");

else if (slot3 == 3)
System.out.println("Bells ");

else if (slot3 == 4)
System.out.println("Melons ");

else if (slot3 == 5)
System.out.println("Bars ");

if (slot1 == slot2 && slot1 == slot3) {
winnings = bet * 3;
System.out.println("You won $" + winnings);
}


else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3) {
winnings = bet * 2;
System.out.println("You won $" + winnings);
}


else
System.out.println("You won $0.");

System.out.print("roll again?");
replay = keyboard.nextLine();
} while(replay.equalsIgnoreCase("y"));

}
}


Anyways Check to see if replay is equal to "y" or actually "y\n" they are two different values. Also read the docs that nextline() will actually grab the newline and insert it into the string or leave it in the stdin buffer. If it is left inside for next call nextLine() will seem to return blank. If this is the case you may have to flush stdin.

I don't know java but this seems like it would be the problem. When in doubt read the docs.

This post was edited by AbDuCt on Nov 5 2014 05:48pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 06:00pm
Quote (AbDuCt @ Nov 5 2014 06:46pm)
God what ever IDE you're using formats your code as if it were autistic.

Code
public class SlotMachine
{
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner keyboard = new Scanner (System.in);
        DecimalFormat cash = new DecimalFormat("#0.00");

        double bet = 1, winnings;
        int slot1, slot2, slot3;
        String replay = "y";

        do {

            System.out.print("How much would you like to bet?"); //problem
            bet = keyboard.nextDouble(); //here

            while(bet<0.01) {
                System.out.print("You must bet at least a penny, re enter a bet.");
                bet = keyboard.nextDouble();
            }


            slot1 = rand.nextInt(5);

            if (slot1 == 0)
                System.out.print("Cherries ");

            else if (slot1 == 1)
                System.out.print("Oranges ");

            else if (slot1 == 2)
                System.out.print("Plums ");

            else if (slot1 == 3)
                System.out.print("Bells ");

            else if (slot1 == 4)
                System.out.print("Melons ");

            else if (slot1 == 5)
                System.out.print("Bars ");


            slot2 = rand.nextInt(5);


            if (slot2 == 0)
                System.out.print("Cherries ");

            else if (slot2 == 1)
                System.out.print("Oranges ");

            else if (slot2 == 2)
                System.out.print("Plums ");

            else if (slot2 == 3)
                System.out.print("Bells ");

            else if (slot2 == 4)
                System.out.print("Melons ");

            else if (slot2 == 5)
                System.out.print("Bars ");


            slot3 = rand.nextInt(5);


            if (slot3 == 0)
                System.out.println("Cherries ");

            else if (slot3 == 1)
                System.out.println("Oranges ");

            else if (slot3 == 2)
                System.out.println("Plums ");

            else if (slot3 == 3)
                System.out.println("Bells ");

            else if (slot3 == 4)
                System.out.println("Melons ");

            else if (slot3 == 5)
                System.out.println("Bars ");

            if (slot1 == slot2 && slot1 == slot3) {
                winnings = bet * 3;
                System.out.println("You won $" + winnings);
            }


            else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3) {
                winnings = bet * 2;
                System.out.println("You won $" + winnings);
            }


            else
                System.out.println("You won $0.");

            System.out.print("roll again?");
            replay = keyboard.nextLine();
        } while(replay.equalsIgnoreCase("y"));

    }
}


Anyways Check to see if replay is equal to "y" or actually "y\n" they are two different values. Also read the docs that nextline() will actually grab the newline and insert it into the string or leave it in the stdin buffer. If it is left inside for next call nextLine() will seem to return blank. If this is the case you may have to flush stdin.

I don't know java but this seems like it would be the problem. When in doubt read the docs.


Scanner.nextLine() reads input up until and including the newline character, and then returns everything but. If you called nextLine() on an input stream which contained "\n" then the String returned would be empty.

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 5 2014 06:25pm
Quote (Minkomonster @ Nov 5 2014 08:00pm)
Scanner.nextLine() reads input up until and including the newline character, and then returns everything but. If you called nextLine() on an input stream which contained "\n" then the String returned would be empty.


Shows you how much java I know.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 06:37pm
Quote (AbDuCt @ Nov 5 2014 07:25pm)
Shows you how much java I know.


I don't know Java either. Every piece of code I have ever posted I have copied from the internet.
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Nov 5 2014 06:49pm
Code
public class SlotMachineSimulation
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in); //Allows input through keyboard.
DecimalFormat cash = new DecimalFormat("#0.00"); //Allows me to make a number value use decimal format I declared by typing cash.format in front of that value.
Random rand = new Random();

String again = "y";
int slot1, slot2, slot3, bet = 1;

do
{
System.out.println("Enter your bet:");
bet = keyboard.nextInt();

while(bet<1)
{
System.out.println("re enter a bet over 0");
bet = keyboard.nextInt();
}

System.out.print("roll again?");
again = keyboard.nextLine();
System.out.println(again);

}while (again.equalsIgnoreCase("y"));


}
}


cut out all the filler, anyone know what to do so it'll read the replay option when it asks for a bet?
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Nov 5 2014 07:09pm
Quote (J-dawg @ Nov 5 2014 06:49pm)
Code
public class SlotMachineSimulation
{
      public static void main(String[] args)
      {
  Scanner keyboard = new Scanner (System.in); //Allows input through keyboard.
  DecimalFormat cash = new DecimalFormat("#0.00"); //Allows me to make a number value use decimal format I declared by typing cash.format in front of that value.
  Random rand = new Random();

  String again = "y";
  int slot1, slot2, slot3, bet = 1;

  do
  {
  System.out.println("Enter your bet:");
  bet = keyboard.nextInt();

  while(bet<1)
    {
    System.out.println("re enter a bet over 0");
    bet = keyboard.nextInt();
    }

  System.out.print("roll again?");
  again = keyboard.nextLine();
  System.out.println(again);

  }while (again.equalsIgnoreCase("y"));


      }
}


cut out all the filler, anyone know what to do so it'll read the replay option when it asks for a bet?


not sure what you mean
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll