Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2008, 10:43 PM
Member
 
Join Date: Oct 2008
Posts: 39
Rep Power: 0
soc86 is on a distinguished road
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);

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-20-2008, 12:27 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 484
Rep Power: 2
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Default
Code:
time=(int)(distance/MOTORWAY);
I recommend you to learn about casting.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-20-2008, 12:38 AM
sai sai is offline
Member
 
Join Date: Nov 2008
Posts: 14
Rep Power: 0
sai is on a distinguished road
Default initialize Distance,time variable
initialize a values to the variables distance and time and try it out
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-20-2008, 01:34 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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:
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:
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?
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Should I use switch or if else hungdukie New To Java 12 01-18-2009 05:21 AM
Help with switch color Daniel AWT / Swing 2 09-18-2008 08:54 AM
How to use Switch keyword Java Tip java.lang 0 04-23-2008 09:07 PM
Switch Statement Help bluegreen7hi New To Java 6 02-06-2008 06:16 AM
Switch Statemet Java Tip Java Tips 0 11-30-2007 10:16 AM


All times are GMT +2. The time now is 07:38 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org