What\'s wrong with the following code? Modify it to produce the intended output.
ID: 3743035 • Letter: W
Question
What's wrong with the following code? Modify it to produce the intended output. Make sure to properly utilize if/else/if statements to avoid redundancy and avoid unnecessary tests. Do not use && or || in your solution.
------
"What percentage did you earn? 87
-----
Java code:
int percent = readInt("What percentage did you earn? ");
if (percent >= 90)
System.out.println("You got an A!");
else (percent >= 80) {
System.out.println("You got a B!");
}
if (percent >= 70) {
System.out.println("You got a C!");
}
if (percent >= 60) {
System.out.println("You got a D!");
}
if (percent < 60) {
System.out.println("You got an F!");
}
Explanation / Answer
"What percentage did you earn? 87
You got a B!"
question code : 5 condition check.
answer following code : 2 condition check.
updated code : avoid redundancy and avoid unnecessary tests.
--------------------------------------------------------------------------------
int percent = readInt("What percentage did you earn? ");
if (percent >= 90){
System.out.println("You got an A!");
}
else if(percent >= 80) {
System.out.println("You got a B!");
}
else if (percent >= 70) {
System.out.println("You got a C!");
}
else if (percent >= 60) {
System.out.println("You got a D!");
}
else {
System.out.println("You got an F!");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.