//This class contains a number of syntax and semantic errors. //Your job is to f
ID: 3821491 • Letter: #
Question
//This class contains a number of syntax and semantic errors.
//Your job is to find them all.
public class DebugMe{
public static void main(String args[]){
printSums(args);
compareBoxes();
}
//This function is designed to print the sums of all numbers between 1 and the
//first number entered as an argument to DebugMe
//For example, if you enter: DebugMe 3
//You should get:
// The sum of the first 1 numbers is 1.
// The sum of the first 2 numbers is 3.
// The sum of the first 3 numbers is 6.
public static void printSums(String[] args){
int count;
count = Integer.parseInt(args);
for (i = 1 ; i <= count ; i++);
{
int sum = 0;
int i = 0;
sum += i - 1;
System.out.println("The sum of the first " + i + " numbers is " + sum + ".");
}
}
}
Explanation / Answer
Hi
I have fixed the issues and highlighted the code changes below
public class DebugMe{
public static void main(String args[]){
printSums(args);
//compareBoxes();
}
//This function is designed to print the sums of all numbers between 1 and the
//first number entered as an argument to DebugMe
//For example, if you enter: DebugMe 3
//You should get:
// The sum of the first 1 numbers is 1.
// The sum of the first 2 numbers is 3.
// The sum of the first 3 numbers is 6.
public static void printSums(String[] args){
int count;
count = Integer.parseInt(args[0]);
int sum = 0;
int i = 0;
for ( i = 1 ; i <= count ; i++)
{
sum += i;
System.out.println("The sum of the first " + i + " numbers is " + sum + ".");
}
}
}
Output:
The sum of the first 1 numbers is 1.
The sum of the first 2 numbers is 3.
The sum of the first 3 numbers is 6.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.