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

Java prablom?


i just started the java course

the techer want us to do a program shows the letter of the grade that you gate in the test


this is the program

import java.util.Scanner;

public class grade
{
public static void main( String arg[] )
{
Scanner input = new Scanner ( System.in );
int g ;
System.out.print( "Enter your grade please:" );
g = input.nextInt();

if ( g <= 49 )
System.out.printf ( " F ");

if ( g >=50 )
System.out.printf ( " C ");

if ( g >=66 )
System.out.printf ( " B ");

if ( g >= 81 )
System.out.printf ( " A ");

if ( g >= 96 )
System.out.printf ( " A+ ");


}


}




the prablom is that when you put a grade like 69 it will show you C B
it should show only C but 69 also works for C condition so what should i do

There is a big difference between "if" and "else if". An if statement will always be executed where as, an "else if" will only be executed when the if returns false. For instance.

if (1 < 2)
System.out.printf ( "True! ");
if (2 < 3)
System.out.printf ( "True! ");

This will print "True! True! ", where as:

if (1 < 2)
System.out.printf ( "True! ");
else if (2 < 3)
System.out.printf ( "True! ");

This will only print "True! "

Get the idea? Now apply it to your project.

First, its good habit to use brackets around your If statement. (the way you did it, does works, but other way is more common in programing).

Second you need to use it IF Else statement. Also I would start from the highest and work your way down.

if ( g >= 96 )
{
System.out.printf ( " A+ ");
}
else if ( g >= 81 )
{
System.out.printf ( " A ");
}
else if .....

this way once you hit the correct grade it drops out of checking the rest.

You can either use nested if
if ( g <= 49 ) {
System.out.printf ( " F ");
} else {
if ( g >=50 )
System.out.printf ( " C ");
}

or

Change the test to include an upper and lower range.
if ( g >= 0 && g <= 49 )
System.out.printf ( " F ");

The logic is wrong.

Read through it as if the score was 85.

It reaches the "g <= 49" line and that is False.

It reaches "g >= 50" line and that is True.

The result is "C".

What you've done is do a series of greater-than-or-equals checks out of order. Reorder them backwards, and it will work.

Edit:
Actually, more needs to be done. Use ElseIf as well.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • What does "line b problem parsing XML: "null" is null or not and object". mean?

    It's a bug in their JavaScript code, nothing you can do apart from report it to AT&T support. It's their problem, not yours.

    ...
  • How to add a chess board in blogspot. i downloaded pgn viewer then wat?

    Try this: ...

  • How to make high quality wallpapers?

    You got it right, Adobe Photoshop is a good software for this.

    ...
  • JAVA what should i do????

    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&#...

  • 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>

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster