Jump to content

Java Program Won't Loop

tp95112
Go to solution Solved by Midnight,

while (stop.equalsIgnoreCase("no"));{

 

get rid of the semicolon.

I am trying to make this program loop when I input "yes" but when it asks me If I want to play again it wont loop. Also the program will take any input but will still perform is in the block

package com.javalab.simple;
import java.util.Random;
import java.util.Scanner;
 
package com.javalab.simple;
import java.util.Random;
import java.util.Scanner;
 
public class whiletest {
public static void main(String[] args) {
String stop="no";
Scanner string = new Scanner (System.in);
System.out.println("Do you want to play slots? ");
stop = string.nextLine();
 
while (stop.equalsIgnoreCase("no"));{
//Slots Rolles
Random rn1 = new Random();
final int SLOT_1 = rn1.nextInt(9);
Random rn2 = new Random();
final int SLOT_2 = rn2.nextInt(9);
Random rn3 = new Random();
final int SLOT_3 = rn3.nextInt(9);
//Output Numbers
System.out.println("You got " + SLOT_1 + " " + SLOT_2 +" " + SLOT_3);
//Play Again?
System.out.print("Do you want to play again?");
stop = string.nextLine();
}    
System.out.print("Maybe Next Time");
}
 
}
 
Link to comment
Share on other sites

Link to post
Share on other sites

while (stop.equalsIgnoreCase("no"));{

 

get rid of the semicolon.

Link to comment
Share on other sites

Link to post
Share on other sites

You can use the same Random object for all of your SLOT constants. The forum also has a code formatting button (The blue <> symbol), just makes it a bit easier to read. I also just made your variables more readable:

import java.util.Random;import java.util.Scanner;public class WhileTest {    public static void main(String[] args) {                String input = "no";        Scanner scan = new Scanner(System.in);        Random rand = new Random();                System.out.println("Do you want to play slots? ");        input = scan.nextLine();        while (input.equalsIgnoreCase("no")) {                        // Slots Rolls            final int SLOT_1 = rand.nextInt(9);            final int SLOT_2 = rand.nextInt(9);            final int SLOT_3 = rand.nextInt(9);                        // Output Numbers            System.out.println("You got " + SLOT_1 + " " + SLOT_2 + " " + SLOT_3);                        // Play Again?            System.out.print("Do you want to play again?");            input = scan.nextLine();        }        System.out.print("Maybe Next Time");    }}

CPU: i7-4790K --- HEATSINK: NZXT Kraken X61 --- MOBO: Asus Z97-A --- GPU: GTX 970 Strix --- RAM: 16GB ADATA XPG --- SSD: 512GB MX100 | 256GB BX200 HDD: 1TB WD Black --- PSU: EVGA SuperNova G2 --- CASE: NZXT H440 --- DISPLAY3 x Dell U2414H --- KEYBOARD: Pok3r (Clears) --- MOUSE: Logitech G Pro --- OS: Windows 10

Link to comment
Share on other sites

Link to post
Share on other sites

snip

 

snip

Thank you. Hate how little syntax error breaks your program. 

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you. Hate how little syntax error breaks your program. 

 

Add a ! to the beginning of the condition of the while loop. Right now the while loop will run only if the user enters "no" which is not what you want based off of how the current question is set up. 

Link to comment
Share on other sites

Link to post
Share on other sites

Add a ! to the beginning of the condition of the while loop. Right now the while loop will run only if the user enters "no" which is not what you want based off of how the current question is set up. 

I ended up changing it to yes. Thanks though

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×