• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Specify a range of numbers, such as from 1 to 10, in an expression?  RSS feed

 
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a program that says different things based on an integer value.  It needs to say one thing if the integer is between 1 and 12, and another if the integer is between 13 and 19, and so on.  Here's the code:



Now let's pretend the preceding code is contained within the required class definition and main() method.  I know that's not how you write a case statement, but I don't know how to define a range of values without defining an entire array.

Also, how does one use an expression in a case statement?  I'm having trouble with that, too.

Thanks.
 
Saloon Keeper
Posts: 5757
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A case can't contain an expression. Forget using switch(), just us if() and else if()s.
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, be careful of you are combining <= and => or similar. It is possible to get some values in the range for multiple options, or to miss some values out of any ranges.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!