this is the quesion teacher ask me in the class) ( Attention: for 4.15, DO NOT w
ID: 3640000 • Letter: T
Question
this is the quesion teacher ask me in the class)
(Attention: for 4.15, DO NOT write the descriptions, I need your correction codes to replace the wrong codes. You can write your correction codes in a .doc or .txt file.)
4.15 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.]
a) if (age >= 65);
System.out.println( "Age is greater than or equal to 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;
Explanation / Answer
The corrected codes are:
a)
if (age >= 65) //no semi colon
System.out.println( "Age is greater than or equal to 65");
----------------------------------------------------------------------
b)
int x = 1, total = 0; //Initialization required
while ( x <= 10)
{
total += x;
x = x + 1;
}
----------------------------------------------------------------------
c)
while ( x<= 100)
{ //parenthesis required
total += x;
x = x+1;
}
----------------------------------------------------------------------
d)
while (y > 0)
{
System.out.println (y)
y = y - 1; //subtracting else it will be an infinite loop
}
---------------------------------------------------------------------
Hope this helps!!!
All The Best!!!
Please do rate me!!!
Thanks!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.