Results 1 to 6 of 6
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Closing/Continuing a while loop **New to Java

    I'm trying to turn off the while loop by using the variables Y and N.

    After the user goes through the while loop for the first time they are suppose to be prompted the question "Would you like to continue Y or N", and I have to get it to where if they select Y it continues if they select N it doesn't

    Java Code:
    import java.util.Scanner; 
    
    public class NumberBounds {
    
    	public static void main(String[] args)  {
    		Scanner keyboard = new Scanner(System.in);
    		
    		boolean continueflag = true;
    
    		while (continueflag)
    		{
    	
    			System.out.println("Enter a number");
    			int starting_number = keyboard.nextInt();
    
    			System.out.println("Enter a Upper Bound");
    			int upper_bound = keyboard.nextInt();
    
    			System.out.println("Enter a addition increment");
    			int step_size = keyboard.nextInt();
    			starting_number += step_size;
    
    			int count = 0;
    
    			while (starting_number <= upper_bound) 
    					{
    			            System.out.print(starting_number + " ");
    			            starting_number += step_size;
    
    			            count += 1; if ((count % 10) == 0)
    			                System.out.println();
    			        }
    			
    				System.out.print("Would you like to continue Y or N?");
    				
    					continueflag = false;
    		}
    
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Closing/Continuing a while loop **New to Java

    I don't see any code that reads in a Y or N or does any kind of logic with it. I'm not sure what you want us to do here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Closing/Continuing a while loop **New to Java

    I need the while loop to be false when a person types in 'No', but continues if they type in 'Yes' instead of it continually starting over and over.

    I've been trying trying ideas for two hours now.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Closing/Continuing a while loop **New to Java

    If you want to do something at least once, it begs for a do ... while loop:

    Java Code:
    do {
       // your ugly deeds
    
       reply= getUserReply();
    }
    while (reply is affirmative);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Closing/Continuing a while loop **New to Java

    concur with jos.

    System.out.print("Would you like to continue Y or N?");

    continueflag = false;

    that automatically sets the flag to false, so not matter what it iterates once.

    have a couple of if's to turn their input into something. as far as I see, they can't even type anything

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Closing/Continuing a while loop **New to Java

    Quote Originally Posted by lenois View Post
    have a couple of if's to turn their input into something. as far as I see, they can't even type anything
    Of course it would help if they read the input in the first place.
    I don't see any use of the scanner after that prompt.
    ;)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java User-Input bug (not continuing); NOVICE
    By Shzylo in forum New To Java
    Replies: 8
    Last Post: 12-13-2012, 06:30 AM
  2. Replies: 5
    Last Post: 10-24-2012, 08:17 PM
  3. Continuing Filenames
    By Stamoulohta in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 07:37 PM
  4. runing Java program increases continuing memory usage
    By henry123 in forum Advanced Java
    Replies: 12
    Last Post: 02-25-2011, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •