File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If-Else to a Switch Statement

 
Paul Hobson
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to rewrite these If-Else Statements into a Switch statement. the problem is i have the >= in the If-Else and everywhere i look. the switch statement corresponds to ==.
char G;
if (N >= 90)
G = 'A';
else if (N>=80)
G = 'B';
else if (N>=70)
G = 'C';
else if (N>=60)
G = 'D';
else
G = 'F';
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, each case of a switch block must be a constant expression - i.e. each case must be a specific (integral) value. This is not the same as allowing boolean conditions to meet some case, which I'd suspect other languages support.
Now, in your specific situation, depending how closely the actual problem matches the posted example, integral division by 10 might be useful, as would thinking about the default case.
 
WY Hsiao
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
There is a program using switch to solve this situation.
Because I am a beginner in java.
If I make a mistake, please tell me and forgive me. ^^
---------------------------------------------------------
[ code removed -ds ]
----------------------------------------------------------
Hope this program can do some help!!^^
Please tolerate my poor English
[ October 31, 2003: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, WY Hsiao! And thanks for your efforts to help out another Rancher.
I removed the code you'd posted as we prefer to help others learn how to figure out problems 'round these parts.
Hope to see you 'round the Ranch!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic