What Am I Doing Wrong With A Slot Machine Game?

• April 21st, 2013, 03:12 PM
zendorz
What Am I Doing Wrong With A Slot Machine Game?
So I am creating a slot machine game were it plays and then picks a random number between 1-10 and then ask the user if he wants to play again if they put in y they play again and if no it stops. So What Am I doing wrong? Sorry the problem is that I can see would you like to play again but I can actually get it to generate the numbers and then have it print out would you like to play again.
Code java:

```<YOUR CODE HERE> package HW10;   import java.util.Scanner;   public class HW10 {     public static void main(String[] args) {   //This header explains what the program does System.out.println("This program randomly generates a number between 1 and 10"); //Declares variables String another = "y"; int slot1; int slot2; int slot3;     //Ask the use to input a yes or a no. Scanner keyboard = new Scanner (System.in);   System.out.print("Would you like to continue y or n:"); another = keyboard.toString();       //Add comments while(another.equalsIgnoreCase("y"));   { slot1 = (int) (Math.random() *(10)); slot2 = (int) (Math.random() *(10)); slot3 =(int) (Math.random() *(10)); } slot1 = slot2 = slot3 = slot2;     }   }```
• April 21st, 2013, 03:16 PM
Norm
Re: What Am I Doing Wrong With A Slot Machine Game?
Can you explain what the problem is? What is the program doing or not doing that is wrong?

[code=java]
[/code]
to get highlighting and preserve formatting.
• April 21st, 2013, 04:32 PM
Skeim
Re: What Am I Doing Wrong With A Slot Machine Game?
as far as I understand u want it to calculate the numbers again if the user types "y", so u just have to suround it all with a while loop, not just slot1. slot2 and slot3, then it will calculate the random numbers until the user write something else than "y" . And u must use the Scanner reference, keyboard to scan the next word, or line, keyboard.nextline() .
like this:
Code java:

```  import java.util.Scanner;   public class HW10 {     public static void main(String[] args) {   //Declares variables String another = "y"; int slot1; int slot2 = 0; int slot3;   while(another.equalsIgnoreCase("y")) { //This header explains what the program does System.out.println("This program randomly generates a number between 1 and 10");   //Ask the use to input a yes or a no. Scanner keyboard = new Scanner (System.in);   System.out.print("Would you like to continue y or n:"); another = keyboard.nextLine();       //Add comments   slot1 = (int) (Math.random() *(10)); slot2 = (int) (Math.random() *(10)); slot3 =(int) (Math.random() *(10));   slot1 = slot2 = slot3 = slot2; }   }   }```
• April 21st, 2013, 05:25 PM
zendorz
Re: What Am I Doing Wrong With A Slot Machine Game?
[QUOTE=Skeim;110441]as far as I understand u want it to calculate the numbers again if the user types "y", so u just have to suround it all with a while loop, not just slot1. slot2 and slot3, then it will calculate the random numbers until the user write something else than "y" . And u must use the Scanner reference, keyboard to scan the next word, or line, keyboard.nextline() .
like this:
[code=java]

import java.util.Scanner;

public class HW10 {

public static void main(String[] args)
{

//Declares variables
String another = "y";
int slot1;
int slot2 = 0;
int slot3;

while(another.equalsIgnoreCase("y")) {
//This header explains what the program does
System.out.println("This program randomly generates a number between 1 and 10");

//Ask the use to input a yes or a no.
Scanner keyboard = new Scanner (System.in);

System.out.print("Would you like to continue y or n:");
another = keyboard.nextLine();

slot1 = (int) (Math.random() *(10));
slot2 = (int) (Math.random() *(10));
slot3 =(int) (Math.random() *(10));

slot1 = slot2 = slot3 = slot2;
}

}

}
Awesome thanks! I I also had to print out the number it generated but that was easy and its working now!