hi, can anyone help me doing a simple while loop in java i need to finish this w
ID: 667716 • Letter: H
Question
hi, can anyone help me doing a simple while loop in java
i need to finish this while loop so when it runs the tests can pass.
// Implement the function so that all tests pass using a while loop. (this is the starting part of the while loop)
public static int addIntsInRangeUsingWhile(int start, int finish)
this are the test, for reference
@Test
public void testAddIntsInRangeUsingWhile() {
assertEquals(10, HelloJava.addIntsInRangeUsingWhile(0,5));
@Test
public void testAddIntsInRangeUsingWhile2() {
int expected = 2 + 3 + 4;
assertEquals(expected, HelloJava.addIntsInRangeUsingWhile(1,5));
}
@Test
public void testAddIntsInRangeUsingWhile3() {
int expected = 3 + 4 + 5;
assertEquals(expected, HelloJava.addIntsInRangeUsingWhile(2,6));
}
Explanation / Answer
public static int addIntsInRangeUsingWhile(int start, int finish)
{
//variable to hold the sum
int sum = 0;
start++;
//writing the end condition
while(start != finish)
{
//adding the number to sum
sum += start;
//incrementing the number
start++;
}
//returning the sum
return sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.