Identify and correct the errors in each of the following pieces of code. [ Note:
ID: 3670018 • Letter: I
Question
Identify and correct the errors in each of the following pieces of code. [Note: There may be more than one error in each piece of code.] Students do not have to run the code, but need to correct the code and comment what the error was and why.
a) if ( age >= 65 );
System.out.println( "Age is greater than or equal to 65" );
else
System.out.println( "Age is less than 65 )";
b) int x = 1, total;
while ( x <= 10 )
{
Total += x;
++x;
}
c) while ( x <= 100 )
total += x;
++x;
d) while ( y > 0 )
{
System.out.println( y );
++y;
Please Help
Explanation / Answer
(a)
if ( age >= 65 );
System.out.println( "Age is greater than or equal to 65" );
else
System.out.println( "Age is less than 65 )";
Please replace the last line with this code that I am giving
System.out.println( "Age is less than 65”);
(b) -----------------------------------------------------------------------------------------------------------------------------------
int x = 1, total;
while ( x <= 10 )
{
Total += x;
++x;
}
In this code total is not initialized Like total=0 or 1
Int x=1,total=0; OR int x,total=1
(c) ------------------------------------------------------------------------------------------------------------------------
while ( x <= 100 )
{
total += x;
}
++x;
(d) -------------------------------------------------------------------------------------------------------------------------
while ( y > 0 )
{
System.out.println( y );
}
++y;
Every loop must be closed if started
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.