Computer problems,Computer help
*AX SOFT>>>Programming & Design

JAVA what should i do????


i have a problem in my java home work

this is the program

import java.util.Scanner;

public class speed
{
public static void main( String arg[] )
{
Scanner input = new Scanner ( System.in );
int s ;
double r;
System.out.print( "Enter the speed please:" );
s = input.nextInt();

r = s * 1.6;

if ( r >= 200 )
System.out.printf ( " This is dangerous! Slow down");

else if ( r >= 150 )
System.out.printf ( " Don't go to fast ");

else if ( r >= 100 )
System.out.printf ( " That's just normal cruising speed ");

}
}



but every time i enter a speed it shows the first sentense ""This is dangerous! Slow down


so where is my mistake

There is nothing wrong with your code. I added the else statement...

else
System.out.println("speed up slow-poke");

and it worked for all entered values. I'm not sure what's going on in your case that would cause it to not work.

I would recommend 2 things.

1) Debug your program by outputting the values to see what they are... before your if/else statement, print out s and r.

2) I follow, the better safe than sorry method by always including wrapping braces { } around my if/else statements.
===========================
Output:
===========================

Enter the speed please:2
speed up slow poke

Enter the speed please:80
That's just normal cruising speed

Enter the speed please:100
Don't go to fast

Enter the speed please:200
This is dangerous! Slow down
================
Edit:
I have to respectfully disagree with quantumuk. The lack of braces will not result in any errors. If/else statements will work fine without them as long as you're just dealing with 1 line of code. They are just good practice because it makes your code more readable and maintainable. I copy and pasted your code exactly as typed, and it compiled and executed without any errors.

Yes, the first poster is right in the suggestions made.

The error in your code is this : you are missing the opening and closing braces for your "if" structure. It should look like this :

if ( r >= 200 )
{
System.out.printf ( " This is dangerous! Slow down");
}
else if ( r >= 150 )
{
System.out.printf ( " Don't go to fast ");
}
else if ( r >= 100 )
{
System.out.printf ( " That's just normal cruising speed ");
}

System.out.printf ( " This is dangerous! Slow down");

Notice the brace right after the "if" statement. When coding any statement or method, I always put the opening and closing curly brace right after I type the condition or name, that way I don't forget to do it later.

Good luck.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • How many bits are in a hash created by the MD5 algorithm?

    c. 128

    ...
  • Deleting Kernel Switch?

    This is an original unmodified boot.ini for WinXP You can freely delete all different stuff [boot loader] timeout=30 default=multi(0)disk(0)rdisk(0)partiti... [operating systems] multi(0)di...

  • I want a job which can be done through Internet and i'm studying java and Algorithms?

    Definitely establish yourself on Rent-a-Coder by completing some jobs. Be prepared for an uphill battle, though. It will require some effort to build a good feedback rating and a job history. The b...

  • What is the html code for linking things?

    <a href="http://www.yahoo.com">Picture</a> <a href="type link to your picture here">Picture</a>

    ...
  • Can a web site database (like Y!A) be on XML files only?

    It is technically possibly - but hideously inefficient. You'd end up replacing expensive database software with even more expensive CPUs and RAM. There are plenty of good, free database pac...

  • Can Frontpage & Dreamweaver Work Together?

    Stick with only one of these, or you will end up with a horrible, mangled, unmaintainable mess. BOTH programs modify the underlying code to suite their own needs. I know as I own and use both of th...

  • Interactive JAVA Tutorials?

    hi twinkle check ...

  • How do i make a button inside a movie clip go back to scene 1 in flash?

    Hello, To move to a specific scene in Flash (actionscript), you would need to use gotoAndPlay( scene, frame ) method. To use it properly, apply the actionscript for that button: Where scene...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster