How do you loop a switch statement?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: How do you loop a switch statement?

  1. #1
    Join Date
    Oct 2005
    Posts
    2

    How do you loop a switch statement?

    I am creating a program that will allow a person to input a month, day and year and this program will convert it to the day of the year. However, I want it to loop until the person puts in a -1. How do i get it to loop?


    public void findCalen(){
    String prompt = "Enter a month or -1 to stop";
    int num1=readInt(prompt);
    if (num1!=-1){
    String dayStr=JOptionPane.showInputDialog(null,"Enter a day");
    int dayA=Integer.parseInt(dayStr);

    String yearStr=JOptionPane.showInputDialog(null,"Enter a year");
    int yearA=Integer.parseInt(yearStr);
    int (yearA % 100 == 0 || yearA % 4 == 0) {
    switch (num1){
    case 1:numOfDays=dayA;
    System.out.println(numOfDays);
    break;
    .
    .
    .
    case 12:numOfDays = 335+dayA;
    System.out.println(numOfDays);}
    }

    else
    switch (num1){
    case 1:numOfDays = dayA;
    System.out.println(numOfDays);
    break;
    .
    .
    .
    case 12:numOfDays = 334+dayA;
    System.out.println(numOfDays);}
    }
    else{
    System.out.println("good bye"); }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Stuff the whole data entry code in a while (true) - loop. Then check each
    integer input inside the loop, if value is -1 then do a break;
    eschew obfuscation

  3. #3
    Join Date
    Oct 2004
    Posts
    311
    cleaner code would be to wo a while(num1!=-1) loop....

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    But the loop contains three user entries of numerics. ( ? )
    eschew obfuscation

Similar Threads

  1. Can you put a loop inside an IF statement?
    By Laurel in forum VB Classic
    Replies: 4
    Last Post: 10-28-2005, 03:15 PM
  2. Problem with a loop
    By salvinger in forum VB Classic
    Replies: 0
    Last Post: 05-07-2005, 02:38 PM
  3. Use of Select Case or Switch Statement
    By Daryl Shockey in forum Database
    Replies: 2
    Last Post: 05-28-2002, 04:50 PM
  4. switch statement
    By James Lin in forum .NET
    Replies: 9
    Last Post: 08-15-2000, 11:54 AM
  5. Replies: 1
    Last Post: 05-02-2000, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center