DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  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, 02:15 PM
  2. Problem with a loop
    By salvinger in forum VB Classic
    Replies: 0
    Last Post: 05-07-2005, 01:38 PM
  3. Use of Select Case or Switch Statement
    By Daryl Shockey in forum Database
    Replies: 2
    Last Post: 05-28-2002, 03:50 PM
  4. switch statement
    By James Lin in forum .NET
    Replies: 9
    Last Post: 08-15-2000, 10:54 AM
  5. Replies: 1
    Last Post: 05-02-2000, 03:29 AM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links