Hack Forums
[Java] Java Switch Statement - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Java Switch Statement (/showthread.php?tid=5949133)



Java Switch Statement - OBAMASELF - Mar 10, 2019

import java.util.Scanner;

public class GradeCalculatorIF
{
   public static void main (String [] args)
   {
       Scanner sc = new Scanner(System.in);

       System.out.println("Please input a student unit total");
       double unitTotal = sc.nextDouble();
       
       if(unitTotal >= 0 && unitTotal < 50)
       {
           System.out.println("F-<m");
       }
       if(unitTotal >= 50 && unitTotal < 60)
       {
           System.out.println("P-5");
       }
       if(unitTotal >= 60 && unitTotal < 70)
       {
           System.out.println("C-6");
       }
       if(unitTotal >= 70 && unitTotal < 80)
       {
           System.out.println("D-7");
       }
       if(unitTotal >= 80 && unitTotal < 90)
       {
           System.out.println("HD-8");
       }
       if(unitTotal >= 90 && unitTotal < 100)
       {
           System.out.println("HD-9");
       }
       if(unitTotal == 100)
       {
           System.out.println("HD-10");
       }
       if(unitTotal > 100 && unitTotal < 0)
       {
           System.out.println("Invalid");
       }
   }
}




How can I change that into a switch statement? Please help a brother out here.


RE: Java Switch Statement - HFDone - Mar 10, 2019

https://www.tutorialspoint.com/java/switch_statement_in_java.htm


RE: Java Switch Statement - k45h - Mar 11, 2019

Switches should be used for switching, not condition checking. You are better off using else-if as it is much clearer.


RE: Java Switch Statement - Suspect. - Mar 11, 2019

Switches should be used when determining which option from multiple options should be used and should not be used with bool statements.


RE: Java Switch Statement - Joseph Stalin - Apr 23, 2019

You cannot possibly use a switch statement in that. Switch statements check for equivalency and equivalency only.