Question

What is wrong with the following code fragment? AND Rewrite it so that it produces correct...

What is wrong with the following code fragment? AND Rewrite it so that it produces correct output.

Java Code:

if (total == MAX)

if (total < sum)

System.out.println(“total == MAX and < sum.”);

else

System.out.println(“total is not equal to MAX”);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Issue here is with curly braces. I have modified the code. It wil work as expected.

when you dont mention the curly braces if statement will treat like single statement. In our case, we did not mention the curly braces, so it treats like with in if condition there is if-else statement. That is the issue here.

if (total == MAX){

if (total < sum)

System.out.println(“total == MAX and < sum.”);

}

else

System.out.println(“total is not equal to MAX”);

Add a comment
Know the answer?
Add Answer to:
What is wrong with the following code fragment? AND Rewrite it so that it produces correct...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT