I just got my java project (beginner). . and its like this

Output:

What is my grade in Java?
(part where you input a grade)
My grade in Math?
(input math grade)

(the next screen shows the grades i input, gets the average, then the shout out of my grade)

JAVA 100
MATH 100
___________
-->AVERAGE 100

YOU ARE GREAT

(then shows this)
DO YOU WANT TO TRY AGAIN [Y/N]?(input)

I got only 1 problem when all is running bellow AVERAGE 100 got odd result

Error:

YOU ARE GREAT

DO YOU WANT TO TRY AGAIN [Y/N]?YOU ARE GREAT

DO YOU WANT TO TRY AGAIN [Y/N]?(input)

My Code:

import java.util.Scanner;
	
	class Test {
	    public static void main(String[] args){
		Scanner myScanner = new Scanner(System.in);
		average = 0;
	String  answer = "Y";
		

   while (answer == "Y"){
	     System.out.println("What is my grade in Java?");
		jgrade = myScanner.nextDouble();
	     System.out.println("\n");
	     System.out.println("My grade in Math?  ");
                mgrade = myScanner.nextDouble();
              
	     System.out.print("JAVA  ");
	     System.out.println(jgrade);
	     System.out.print("Math  ");
	     System.out.println(mgrade);
             System.out.println("____________");
                 average = (jgrade + mgrade) / 2;
             System.out.print("AVERAGE  ");
             System.out.println(average);

      while (average < 101);
          if (average >=95){
		System.out.println("YOU ARE GREAT");
         else if (average >=90)
		System.out.println("YOU GOT GOOD GRADES");
         else if (average >=85)
		System.out.println("NOT BAD FOR A BEGINNER");
	 else if (average >=80)
		System.out.println("YOUR A BEGINNER");
	 else if (average >=75)
		System.out.println("YOU PASSED! CONGRATULATIONS");
	 else if(average >=50)
		System.out.println("PERFECT ABSENT");
	    System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]?   ");
		answer = myScanner.nextline();
		
	           }//if
		}//while
          }
  }

Please reply asap T_T

Edited 6 Years Ago by walter307: n/a

Your program can not be compiled successfully.
Where is your declaration for average,jgrade...
the code in line 26 is a dead loop if (average < 101)
The last curly brace is redundant that should be removed.

Your program can not be compiled successfully.
Where is your declaration for average,jgrade...

jgrade = myScanner.nextDouble();
mgrade = myScanner.nextDouble();
(average = (jgrade + mgrade) / 2);

the code in line 26 is a dead loop if (average < 101)
(how come a dead loop? i did declare a average if not please clarify)

The last curly brace is redundant that should be removed.
(its just fine i got 4 open braces)

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Walter - obviously, you've typed this code in and mistyped some things. The variables Tong is pointing to are not declared in this code. However, they would have to be declared in the code you're running, or you would be asking why this program doesn't run. The lines you pull out there aren't declarations, but there must be a line there with

double jgrade;

or

double jgrade = myScanner.nextDouble();

So that doesn't need to be fixed - the code you've posted is wrong, but we know what it must be, so let's go on to the trouble spots.

Line 26 would compile, and if average is less than 101, your program would go into an endless loop, doing nothing, and your program would just stop. Since average is less than 101 in your examples, and that's not what your program is doing, it's not what you have in your program. Probably in your program you have that missing left curly brace that Tong pointed out. So suppose line 26 actually reads

while (average<101) {

Ah, that'd be why you're having the problem you're seeing.

You've got a while loop that checks whether (average <101) and then it prints out a congratulatory message and asks for input. Then it checks the statement again - it's still true, so it repeats. Forever.

So, there's your problem. Please try to fix it yourself before you ask how - part of that whole "showing some effort" thing.

Also, in future postings, please use the CODE-tags and proofread your code. Thanks!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

thank you for the fast reply il be re-editing my codes as soon as i figured out something. .

i re edited my codes still the out put in the congratulatory part is still there

YOU ARE GREAT

DO YOU WANT TO TRY AGAIN [Y/N]?YOU ARE GREAT

DO YOU WANT TO TRY AGAIN [Y/N]?

it still repeated itself twice on that area only

while (average < 101){
          if (average >=95){
		System.out.println("YOU ARE GREAT");
         else if (average >=90)
		System.out.println("YOU GOT GOOD GRADES");
         else if (average >=85)
		System.out.println("NOT BAD FOR A BEGINNER");
	 else if (average >=80)
		System.out.println("YOUR A BEGINNER");
	 else if (average >=75)
		System.out.println("YOU PASSED! CONGRATULATIONS");
	 else if(average >=50)
		System.out.println("PERFECT ABSENT");
	    System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]?   ");
		answer = myScanner.nextline();

consider the close braces

I've looked at your code again, and I don't believe that that's the code you're running. I may be wrong, but I don't see any way that code - assuming the curly brace is fixed and the variable declarations put in order - doesn't go into an endless cycle of printing your grade, asking for a "Y" or a nay, and then repeating. So either I'm tired and I'm wrong, or I'm tired and I'm right. In either case, my probable next step is to go to sleep. Your probable next step is to trace the code that you're actually running and see what might make it do this little double loop, and possibly to copy and paste the code you're actually running into this thread (in code tags) and maybe someone will see what the problem is in the morning.

May I suggest in terms of your first post:
(1) replace line 10 by

while (answer.compareToIgnoreCase("Y")==0){

since tne comparison" answer == "Y" is the conpare references (addresses) of two instances.
(2) remove line 26 which seems useless
(3) line 40 should be:

answer = myScanner.nextLine();

the L in the method named "nextLine()" should be upper case.
I put one more line code after line 40:
answer = myScanner.nextLine();
( to "eat" the character '\n'???)
It works.

Edited 6 Years Ago by tong1: n/a

Or, The 40 line should be

answer = myScanner.next();

Then the program works. Remember, you should use the method next() instead of readLine() in this case.

Edited 6 Years Ago by tong1: n/a

import java.util.Scanner;
           class IT {
              public static void main(String[] args){
                    Scanner myScanner = new Scanner(System.in);
                    double average;
                    String answer = "Y";
                    double jgrade, mgrade;


while (answer == "Y"){
System.out.println("What is my grade in Java?");
jgrade = myScanner.nextDouble();
System.out.println("\n");
System.out.println("My grade in Math? ");
mgrade = myScanner.nextDouble();

System.out.print("JAVA ");
System.out.println(jgrade);
System.out.print("Math ");
System.out.println(mgrade);
System.out.println("_____________");
average = (jgrade + mgrade) / 2;
System.out.print("AVERAGE ");
System.out.println(average);

while (average < 101){
if (average >=95){
System.out.println("YOU ARE GREAT");
}
else if (average >=90)
System.out.println("YOU GOT GOOD GRADES");
else if(average >=85)
System.out.println("NOT BAD FOR A BEGINNER");
else if (average >=80)
System.out.println("YOUR A BEGINNER");
else if (average >=75)
System.out.println("YOU PASSED! CONGRATULATIONS");
else if(average >=50)
System.out.println("PERFECT ABSENT");
System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]?");
answer = myScanner.nextLine();
}
}
}
}

thats the code i use please if anyone correct me if im wrong.. i only used basic ones

ok mr. tong im recompiling the codes and study the meaning of the codes you suggest. the one in line 10. sorry for the late reply. .

while (answer.compareToIgnoreCase("Y")==0){

awts dont mind line 29 cant remove it sorry XD

what about i set a control on the average to 100 limit.

thats the reason i put

while (average <101)

to control it on 100 only

Edited 6 Years Ago by walter307: n/a

ok im done its all running w/o any errors on it heres the complete code..

import java.util.Scanner;
	
	class Test {
	    public static void main(String[] args){
		Scanner myScanner = new Scanner(System.in);
		double average;
	String  answer = "Y";
		double jgrade, mgrade;

   while (answer.compareToIgnoreCase("Y")==0){
	     System.out.println("What is my grade in Java?");
		jgrade = myScanner.nextDouble();
	     System.out.println("\n");
	     System.out.println("My grade in Math?");
                mgrade = myScanner.nextDouble();
              
	     System.out.print("JAVA  ");
	     System.out.println(jgrade);
	     System.out.print("Math  ");
	     System.out.println(mgrade);
             System.out.println("____________");
                 average = (jgrade + mgrade) / 2;
             System.out.print("AVERAGE  ");
             System.out.println(average);

          if (average >=95)
		System.out.println("YOU ARE GREAT");
         else if (average >=90)
		System.out.println("YOU GOT GOOD GRADES");
         else if (average >=85)
		System.out.println("NOT BAD FOR A BEGINNER");
	 else if (average >=80)
		System.out.println("YOUR A BEGINNER");
	 else if (average >=75)
		System.out.println("YOU PASSED! CONGRATULATIONS");
	 else if(average >=50)
		System.out.println("PERFECT ABSENT");
	    System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]?   ");
		answer = myScanner.next();
	}
    }
}

I'll give credit to those who helped and replied with their ideas especially tong1.
Thank you so much people! By the way, Its a nice Forum ^_^

You'll be hearing a lot from me from now on. So please be gentle hehehe ^_^
Again thank you!


~Solved~

Edited 6 Years Ago by walter307: n/a

This question has already been answered. Start a new discussion instead.