Write a method header on line two with the following specs:
Returns: an integer Name: sumToX Parameters: an integer called "x" Purpose: calculate the sum of the integers from 1 to x (including x) |
Examples:
- sumToX(5) ==> 15
- sumToX(7) ==> 28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Main {
public static int sumToX(int x)
{
int sum=0;
for(int i=1;i<=x;i++) {
sum+=i;
}
return sum;
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(sumToX(5)); //15
System.out.println(sumToX(7)); //28
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
048 - Accumulator Method Practice 4 (0) | 2019.12.26 |
---|---|
047- Accumulator Method Practice 3 (0) | 2019.12.26 |
044 - Method Header Challenge 1 (Optional) (0) | 2019.12.19 |
043 - Method Header Practice 8 (0) | 2019.12.19 |
042 - Method Header Practice 7 (0) | 2019.12.19 |