Results 1 to 2 of 2
  1. #1
    BAD
    BAD is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default Help with creating a while loop!

    Okay, my program works fine, but I want my program to loop when the user inputs a "Y" for Yes, or a "N" for No for the program to terminate. Here is my code:

    Java Code:
    import java.util.*;
    public class Main {
        public static void main(String[] args)
        {
        // Declared variables.
        String name;
        String Y;
        String N;
        String Yes = "Y";
        String No = "N";
        String grade1 = "Freshman";
        String grade2 = "Sophomore";
        String grade3 = "Junior";
        String grade4 = "Senior";
        String grade5 = "Graduate Student";
        String name1 = "Alabama";
        String name2 = "Alaska";
        String name3 = "Arkansas";
        String name4 = "Arizona";
        String name5 = "California";
        String name6 = "Colorado";
        String name7 = "Delaware";
        String name8 = "Florida";
        String name9 = "Georgia"; 
        String name10 = "Idaho";
        int count, Error = 1;
        int grade;
    
    // User input prompt
    Scanner scan = new Scanner (System.in);
         
    System.out.print("Enter your name: ");
    name = scan.nextLine();
    System.out.print("Enter your classification: ");
    grade = scan.nextInt();
    
    switch (grade)
        {
            case 1:
            System.out.println();
            System.out.println("This output is produced by " + name + ", who is a "
                               + grade1 + " at UNF.");
            System.out.println();
            System.out.println();
            break;
    
            case 2:
            System.out.println();
            System.out.println("This output is produced by " + name + ", who is a "
                               + grade2 + " at UNF.");
            System.out.println();
            System.out.println();
            break;
             
            case 3:
            System.out.println();
            System.out.println("This output is produced by " + name + ", who is a "
                               + grade3 + " at UNF.");
            System.out.println();
            System.out.println();
            break;
             
            case 4:
            System.out.println();
            System.out.println("This output is produced by " + name + ", who is a "
                               + grade4 + " at UNF.");
            System.out.println();
            System.out.println();
            break;
             
            case 5:
            System.out.println();
            System.out.println("This output is produced by " + name + ", who is a "
                               + grade5 + " at UNF.");
            System.out.println();
            System.out.println();
            break;
        }// end switch
    
    [COLOR="Red"][B]THIS IS WHERE I WANT TO INPUT MY LOOP STATEMENT, BUT I DON'T KNOW HOW TO DO IT!!![/B][/COLOR]
            
    
    System.out.print("Enter an integer from 1 to 10: ");
    count = scan.nextInt();
    int repeat = 0;
    while(count < 1 || count > 10)
        {
            System.out.println();
            System.out.println("Try again. Enter an integer between 1 and 10: ");
            count = scan.nextInt();
            if (count < 1 || count > 10)
            repeat++;
            if (repeat == 2)
            {
                System.out.println();
                System.out.println("Get a life! Program Terminated.");
                System.exit(Error);
            }// end if statement
        }// end while
    
       
    
    
    switch (count)
        {
            case 1:
            System.out.println();
            System.out.println("The States from 1 down are: \n\t" + name1 + "\n\t"
                                + name2 + "\n\t" + name3 + "\n\t" + name4 + "\n\t"
                                + name5 + "\n\t" + name6 + "\n\t" + name7 + "\n\t"
                                + name8 + "\n\t" + name9 + "\n\t" + name10);
            break;
    
            case 2:
            System.out.println();
            System.out.println("The States from 2 down are: \n\t" + name2 + "\n\t"
                                + name3 + "\n\t" + name4 + "\n\t" + name5 + "\n\t"
                                + name6 + "\n\t" + name7 + "\n\t" + name8 + "\n\t"
                                + name9 + "\n\t" + name10);
            break;
    
            case 3:
            System.out.println();
            System.out.println("The States from 3 down are: \n\t" + name3 + "\n\t"
                                + name4 + "\n\t" + name5 + "\n\t" + name6 + "\n\t"
                                + name7 + "\n\t" + name8 + "\n\t" + name9 + "\n\t"
                                + name10);
            break;
    
            case 4:
            System.out.println();
            System.out.println("The States from 4 down are: \n\t" + name4 + "\n\t" 
                               + name5 + "\n\t" + name6 + "\n\t" + name7 + "\n\t" +
                               name8 + "\n\t" + name9 + "\n\t" + name10);
            break;
            
            case 5:
            System.out.println();
            System.out.println("The States from 5 down are: \n\t" + name5 + "\n\t" 
                               + name6 + "\n\t" + name7 + "\n\t" + name8 + "\n\t" +
                               name9 + "\n\t" + name10);
            break;
            
            case 6:
            System.out.println();
            System.out.println("The States from 6 down are: \n\t" + name6 + "\n\t" 
                               + name7 + "\n\t" + name8 + "\n\t" + name9 + "\n\t"
                               + name10);
            break;
            
            case 7:
            System.out.println();
            System.out.println("The States from 7 down are: \n\t" + name7 + "\n\t"
                               + name8 + "\n\t" + name9 + "\n\t" + name10);
            break;
           
            case 8:
            System.out.println();
            System.out.println("The States from 8 down are: \n\t" + name8 + "\n\t"
                               + name9 + "\n\t" + name10);
            break;
            
            case 9:
            System.out.println();
            System.out.println("The States from 9 down are: \n\t" + name9 +
                               "\n\t" + name10);
            break;
    
            case 10:
            System.out.println();
            System.out.println("The State from 10 is: \n\t" + name10);
            break;
        }// end switch  
    
    
    System.out.println("Do you wish to execute the select again? Input Y for Yes, "
                       + "or N for No: ");
    Y = scan.nextLine();
    
    
    
        
            
        }// end main()
    }// end class


    If anyone can help me out here then that would be great!

    Moderator Edit: Code tags added
    Last edited by Fubarable; 07-09-2010 at 10:25 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default

    Please use code tags around your code to preserve code formatting.

    Pseudo code:
    Java Code:
    while(true) {
      prompt user for response
      if response is No
         System.exit(0); // exit program / terminate
     ... do rest of stuff ...
    } // end while(true)

Similar Threads

  1. Creating a Loop
    By SenorJalapeno in forum New To Java
    Replies: 3
    Last Post: 04-02-2010, 10:13 AM
  2. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM
  3. Replies: 3
    Last Post: 07-23-2009, 07:45 AM
  4. Replies: 1
    Last Post: 07-22-2009, 10:10 PM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 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
  •