• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Greenhorn
Posts: 8
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: 5472
55
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: 62819
203
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!