java - Not able to get this error message to work -
the code compiles , until come in 101 , shows grade is: . doing wrong?
public static void main(string args[]) { scanner sc = new scanner(system.in); int yourgrade; char grade = 0; system.out.print("enter grade (between 0 , 100 inclusive): "); yourgrade = sc.nextint(); if ((yourgrade>0) && (yourgrade<40)) { grade='f'; } else if ((yourgrade>39) && (yourgrade<55)) { grade='d'; } else if ((yourgrade>54) && (yourgrade<70)) { grade='c'; } else if ((yourgrade>69) && (yourgrade<85)) { grade='b'; } else if ((yourgrade>84) && (yourgrade<=100)) { grade='a'; } if ((yourgrade<0) && (yourgrade>100)) { system.out.println(" " + grade + " not valid grade."); system.out.println("please come in value between 0 , 100 inclusive."); } else { system.out.println("your grade is: " + grade); }
i tried adding lastly bit before final if statement yet, still not able display want to. please forgive me if doing wrong started , know how right.
if ((yourgrade<0) && (yourgrade>100))
these 2 conditions can't both true. if want check if 1 or other true, utilize or instead of and.
if (yourgrade<=0 || yourgrade>100)
also changed <0
<=0
since grade conditions f don't include zero.
you might notice in case, grade
variable still set character zero, because that's how initialised it. printing
system.out.println(" " + grade + " not valid grade.");
won't create much sense.
java
No comments:
Post a Comment