• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Something is missing in my Switch method

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






the instruction to this code is to make the user write numbers from 1-100 and the application should categorize the numbers in the following way



100 - A
90-99 - B
80-89 - C
70-79 - D
60-69 - E
other - F


the problem is with the other. it should go from 59 to 0.

however, the application categorizes any number as "others"

how can i cause the application to accept numbers from 0-100 and not accept numbers higher than 100? [/code]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a series of if-statements:
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.




This exercise is only on the switch function. you are not allowed to use if or while or any other fancy method..


i think i am close to the answer, but something is missing to make the "other" -10 to 50. alternatively , make the input go to 100 and not above
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have certain requirements for a particular problem, you need to state them up front--how else can we be expected to help in any reasonable way?

So you have at least a couple of options: you could do some match to make the switch statement work without listing all the numbers, or you could just list all the numbers.

Do you have any *other* requirements we need to know?
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dmitri Makovetskiy wrote:

David Newton wrote:Or: think carefully about what a switch statement does--it can only compare a single value at a time. You, on the other hand, want to compare a range of values. Rob's answer is correct, but I'd rather you took the time to consider *why* the switch statement wasn't working, and why Rob's answer does.




This exercise is only on the switch function. you are not allowed to use if or while or any other fancy method..


i think i am close to the answer, but something is missing to make the "other" -10 to 50. alternatively , make the input go to 100 and not above





why cant i write before the case 60:




and at the end

default:
System.out.println("invalid number");
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David no. There is a line that i need to add, i think it should prevent the numbers going over 100.

the way i listed above, is a bit unwanted, cause i was instructed to make 5 cases and not 10...
but if i have no choice, i will have to go with the way above.

is there a way to make the numbers go till 100?


if i put somewhere near the end this statement:
number=100

would it work?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to check the number for validity *before* entering the switch statement.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to do this just with a switch block, you need to specify 100-69 = 31 case items.
If not, you should use some conditions like if-checks or ternary-if-checks.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(@Devaka: not necessarily--check the math in the original post.)
 
Devaka Cooray
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:(@Devaka: not necessarily--check the math in the original post.)


Oh ya, thanks!
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic