KnowledgeBoat Logo
LoginJOIN NOW

Computer Applications

The following code segment should print "You can go out" if you have done your homework (dh) and cleaned your room (cr). However, the code has errors. Fix the code so that it compiles and runs correctly.

boolean dh = True; 
boolean cr= true; 
if (dh && cr)
System.out.println("You cannot go out"); 
else
System.out.println("You can go out");	

Java Conditional Stmts

ICSE Sp 2024

56 Likes

Answer

The corrected code is as follows:

boolean dh = true; 
boolean cr= true; 
if (dh && cr)
System.out.println("You can go out"); 
else
System.out.println("You cannot go out");	
Explanation
  1. boolean dh = True; — Here, True should be in lowercase as true.
  2. println statements of if-else should be interchanged because if both dh (done homework) and cr (cleaned room) are true then the person can go out.

Answered By

33 Likes


Related Questions