Results 1 to 4 of 4
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default What is wrong with my switch??

    What is wrong with this switch in my coding, keeps stating there is a problem with (time=distance/MOTORWAY)


    */
    public class Travel{

    public static void main (String[]args){

    char road;
    double distance;
    final int MOTORWAY=85;
    final int A=70;
    final int B=55;
    final int URBAN=40;
    int time;

    int minutes;
    int seconds;

    seconds =(time*3600);
    seconds = seconds%60;

    minutes =time-(int)time;
    minutes=minutes*60;

    switch (time){

    case 'm':
    case 'M': time=distance/MOTORWAY;
    System.out.println("To travel "+distance+ "road type m takes "+time+" seconds");

    break;

    case 'a':
    case 'A': time= distance/A;
    System.out.println("To travel "+distance+ "road type a takes "+time+" seconds");

    break;

    case 'b':
    case 'B': time= distance/B;
    System.out.println("To travel "+distance+ "road type b takes "+time+" seconds");

    break;

    case 'u':
    case 'U': time= distance/URBAN;
    System.out.println("To travel "+distance+ "road type u takes "+time+" seconds");

    break;

    default: System.out.println("Invalid road type x");
    System.exit(0);

    }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    time=(int)(distance/MOTORWAY);
    I recommend you to learn about casting.

  3. #3
    sai
    sai is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default initialize Distance,time variable

    initialize a values to the variables distance and time and try it out

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Some comments...

    There are various things wrong with your program, so bear with me...
    • You are trying to do calculations with variables that have not been assigns values:

    Java Code:
    case 'M': time=distance/MOTORWAY;
    in this case, distance hasn't been assigned a value yet (check the rest of your program for similiar cases)

    • You are trying to assign more than one value to a same variable:

    Java Code:
    seconds =(time*3600);
    seconds = seconds%60;
    Either you select which value the variable "seconds" is going to have or rename one of the variables. Do you mean something like the following?
    Java Code:
    seconds1 =(time*3600);
    seconds2 = seconds1%60;
    The "minutes" variable has this same problem.
    • The case statements only work with integers, not with characters

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. [SOLVED] Should I use switch or if else
    By hungdukie in forum New To Java
    Replies: 14
    Last Post: 12-25-2011, 01:49 AM
  2. Help with switch color
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 09-18-2008, 07:54 AM
  3. How to use Switch keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:07 PM
  4. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  5. Switch Statemet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-30-2007, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •