Write a method sumRange which takes two integer parameters, and sums all the integers between them (inclusive) and returns the sum as an integer.
If the second number is smaller than the first, the method should return 0 and display an error message.
....
to issue an error message, print it to the console.... like this:
System.Out.Println("Error!!! The second value was less than the first in method sumRange!");
Here are some example calls to sumRange and what they would return:
sumRange(1,3) should return 6 - 1+2+3
sumRange(2,4) should return 9 - 2+3+4
sumRange(3,3) should return 3 ... just 3
sumRange(4,2) should return 0, and print the error message to the console.
Paste your sumRange method into the assignment window here.
all I have is:
|
Code:
|
public statis int sumRange(int start, int end) //this should be the parameters
{
int sum = 0
if (end < start)
system.out printIn ("ERROR: Invalid Range");
else
for (int num = start; num < = end; num++)
sum + = num;
return ;
} |
I know this is wrong.