0

Can you spot what is wrong with the java code that I am doing. Here is the java code that I have been working on...

import java.io.*;
public class Konata
{
public static void main
(String args [])
{
BufferedReader br=newInputStreamReader(System.in);
int grade,answer=0;
do
{
System.out.print ("\n Enter grade");
grade=Integer.parseInt(br.readline());
if ((grade>=90)||(<=100))
System.out.print("\n A");
else if ((grade>=80)||(<=89))
System.out.print("\n B");
else if ((grade>=70)||(<=79))
System.out.print("\n C");
else if ((grade>=60)||(<=69))
System.out.print("\n F");
else 
System.out.print("\n out of range");
System.out.print("\n Do you want to try again? Press 1 for Yes and Press 0 for No:");
answer=Integer.parseInt(br.readLine());
}while (answer==1);
}
}

whatever number i type, the only answer read is A? thank you so much for the help!

Edited by akasekaihime: n/a

5
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by ztini
2

the if statements are incorrect; in Java, you cannot compare a range the way you are trying to do. This is a common misunderstanding, as it makes sense mathematically, but doesn't work in most programming languages.

You need to write out a full clause on each side of the or operator:

import java.io.*;
public class Konata {
    public static void main(String args []) {
        BufferedReader br=newInputStreamReader(System.in);
        int grade,answer=0;

        do {
            System.out.print ("\n Enter grade");
            grade=Integer.parseInt(br.readline());

            if ((grade>=90)||(grade<=100)) {
                System.out.print("\n A");
            }
            else if ((grade>=80)||(grade<=89)) {
                System.out.print("\n B");
            }
            else if ((grade>=70)||(grade<=79)) {
                System.out.print("\n C");
            }
            else if ((grade>=0)||(grade<=69)) {
                System.out.print("\n F");
            }
            else { 
                System.out.print("\n out of range");
            }

            System.out.print("\n Do you want to try again? Press 1 for Yes and Press 0 for No:");
            answer=Integer.parseInt(br.readLine());
        }while (answer==1);
    }
}

Note that I reformatted the code according to the Java standard. You should generally follow this standard, as it makes the code more familiar and more readable.

1
if ((grade>=90)||(grade<=100)) {)

Does this return true if grade is 20?
(note: 20 is < 100 so the second condition is true and the whole OR connected condition is true)
Do you want both conditions to be true or only one of them?
Both requires AND
only one requires OR

-1

You can drop a lot of the logic if you check for bad conditions first. There is no right answer, but this might be a bit cleaner.

if (grade > 100 || grade < 0)
                System.out.print("\n out of range");
            else if (grade >= 90 )
                System.out.print("\n A");
            else if (grade >= 80)
                System.out.print("\n B");
            else if (grade >= 70)
                System.out.print("\n C");
            else if (grade >= 60)
                System.out.print("\n D");
            else
                System.out.print("\n F");
1

Also, instead of doing:

System.out.print("\n rawr");

You could use:

System.out.println("rawr");

The println does a return line after the "rawr". This is a little different from yours which returns before the line. However, I found that carefully placing printlns eliminates the need for '\n' almost all together.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.