Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: What Am I Doing Wrong With A Slot Machine Game?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    <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;
     
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,816
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default 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?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
     
    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;
                }
     
    	}
     
    }

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();



    //Add comments

    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!

Similar Threads

  1. Slot Machine game in Java. using Net Beans.
    By leo24 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 28th, 2012, 11:32 PM
  2. Slot machine, animating the reels to spin
    By LukeDavison in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2011, 10:59 AM
  3. Slot Machine
    By nemoisback66 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 17th, 2010, 01:57 PM
  4. Replies: 8
    Last Post: February 24th, 2009, 04:04 PM