Question:
not getting desired output for switch statement in Java?
Don
2021-02-04 04:51:16 UTC
I'm currently trying to self teach Java. After watching tutorials, I have attempted a switch statement. Unfortunately one screen shot  will not capture all of the coding that I used for this. In my last system.out.println statement, I put in (" your weight is " + weight). Instead of the desired output of "your weight is bantamweight". I got the output of " your weight is 135". What can I do to get it so that it states the weight class as opposed to the weight itself and can this be done with the weight ranges I use (ex: weight >=125 && weight < 135. Anything to point me in the right direction would be appreciated. If this message isn't precise enough, please let me know.
Three answers:
EddieJ
2021-02-04 04:59:16 UTC
A statement is inappropriate for this task.



You would need

case 135:



And you would need such a case for each possible weight.



You should just use statements, except you should also use to avoid having 2 tests in each .  Once you've eliminated a range, you shouldn't have to test for the starting point.
Jogger2425
2021-02-04 11:12:28 UTC
You don't understand how "switch" is used in Java. When you understand how "switch" works, I think you'll realize "switch"  is not the best way to solve your example.



When the code says  "switch (foo) {" , each value after the "case" is a potential value for "foo". I'm assuming the weights are in pounds. Your code is testing "weight" for 1 pound, then for 2 pounds, then for 3 pounds, then for 4 pounds. 





switch (foo) {

    case 7:      

         // something to do when foo == 7

    break;



   case 10:

   case 11:

   case 12:  

      // something to do when foo is == 10 or == 11 or == 12

   break;

    

  default:

     // something to do when foo is not equal to any of the above

   break; 





To fix your code, using "switch" instead of the "if" statements, part of it  might read thus:

     case 125: case 126: case 127: case 128:  case 129:

     case 130: case 131: case 132: case 133:  case 134: 

           System.out.println ("bantamweight");

     break;



You would do the same for each weight class



Your code lacks the "break" statements after each "case". The "break" statement tells the program to skip the rest of the cases, and jump to whatever comes after the switch code block. What's worse, without a "break", it falls through into the next "case" blocks, and executes the code there, without retesting the "switch" value. It keeps falling through until it reaches the end of the "switch" block or a "break" statement.



The easiest way to fix your code is to delete the "switch (weight)" and all the "case" statements. You might want to change the "if" statements to "else if", except for the first. Remove the "default:" statement, and precede the print with "else". 



Here is a hypothetical example of using "switch" for a college student who sets the alarm one hour before the start time of his first class of the day.  The days of weeks are constants. 



switch (dayOfWeek) {



     case MONDAY:

     case WEDNESDAY:

     case FRIDAY:

        myAlarm.setTime ("09:00");  // setting a time turns on alarm if it is off

        break;

 

     case TUESDAY:

     case THURSDAY:

        myAlarm.setTime ("06:45");

        break;



    case SUNDAY:

    case SATURDAY:

         myAlarm.setOn (false);

         break;



    // no default;



}
anonymous
2021-02-04 06:01:04 UTC
the 2nd part is what you want?  



public class Program

{



  public static void main (String args[])

  {

    int weight = 135;

    if (weight < 115)

      {

 System.out.println ("streamWeight");

      }

    else if (weight >= 115 && weight < 125)

      {

 System.out.println ("flyWeight");

      }

    else if (weight >= 125 && weight < 135)

      {

 System.out.println ("bantamWeight");

      }

    else if (weight >= 135 && weight < 145)

      {

 System.out.println ("featherWeight");

      }

    else

      {

 System.out.println ("elseWeight");

      }





//// 2nd part

    // int condition = 1;

    int condition = (weight < 115) ? 1 : (weight >= 115 && weight < 125) ? 2 : (weight >= 125 && weight < 135) ? 3 : (weight >= 135 && weight < 145) ? 4 : 0;

    System.out.println (condition);

    switch (condition)

      {

      case 1:

 System.out.println ("streamWeight");

 break;

      case 2:

 System.out.println ("flyWeight");

 break;

      case 3:

 System.out.println ("bantamWeight");

 break;

      case 4:

 System.out.println ("featherWeight");

 break;

      default:

 System.out.println ("elseWeight");

 break;

      }

  }

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...