/*Additional selection structures 16/3/2015 Programme to display weight on a given planet*/


import java.util.*;

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

      int weight;
      char planet;
      double planet_weight;

      System.out.print ("Enter your weight");
      weight = keyboardIn.nextInt ();

      System.out.print ("Enter your planet of choice");
      planet = keyboardIn.next().charAt(0);
      switch (weight)
      {
         case 'M': case 'm':
         planet_weight = weight * 0.4; 

         System.out.print ("Your weight on Mercury is"+ planet_weight);
         break;

         case 'V': case 'v':
         planet_weight = weight * 0.9;
         System.out.print ("Your weight on Venus is"+ planet_weight);
         break;

         case 'J': case 'j':
         planet_weight = weight * 2.5;
         System.out.print ("Your weight on Jupiter is"+ planet_weight);

         case 'S': case 's':
         planet_weight = weight * 1.1;
         System.out.print ("Your weight on Saturn is"+ planet_weight);

         default:
         System.out.print ("Invalid entry");
      }
   }
}

This is the piece of code I'm working with. It has to be done in a switch structure. The problem I am having is that after asking for the input for weight and and planet it skips all the options and goes straight to incalid entry. I feel like I am missing something really obvious but this is the first time we have had to do a switch structure with a calculations.

Thanks

Each case statement needs a 'break;' command at the end, otherwise it will fall through to the next, resulting in your experienced output. Also, I would suggest that you to a case-conversion for weight in the switch() statement so you don't need stuff like case 'S': case 's': constructs.

Some of the cases do have breaks, but that's not the real problem.

(sorry Ant695 but you're going to kick yourself when you read this...)
You should be switching on the chosen planet, not the person's weight!
J

I added in the break that I missed but it is still falling through.

Thanks james that solved it. I knew it was something simple but I read over it at least 20 times and couldn't see it.

We've all been there and done that :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.