• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Switch Case Statement Question Problem

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I add here Invalid Size Number if it's less than 1 and more than 12?
I tried while loop but i still get the Size right after i get the "Invalid Size Number" message.

By the way, before you say it.Yes i have to use the JOptionPane for this question.

Question Instructions:

Display the message Size is XS if size is 1 or 2 or 3.
Display the message Size is XS if size is 4 or 5 or 6.
Display the message Size is XS if size is 7 or 8 or 9.
Display the message Size is XS if size is 10 or 11 or 12.

Display the message Invalid Size Number


 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't divide. Use fallthroughNote:
  • 1: If you use fallthrough always write comments that you intend the fallthrough. Otherwise people maintaining the program will assume it is a mistake.
  • 2: You cannot put break; after throw.
  • Another way to do it is with if‑elses. That may actually look more elegant.
     
    Marshal
    Posts: 8490
    610
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In addition to CR suggestions, probably think of creating a suitable separate method to define a dress size and print it to console or MessageDialog of JOptionPane. Don't cram everything into one main method. Always try to decompose your whole code into small few lines long methods and name them appropriately, so they'd reveal their actual intention.

    Code indenation also could be improved as it is not consistent. Check Java Code Conventions (<- link). It is more than 15 years old, but still being applied regularly.
     
    So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic