Sunday
May 24, 2015

Homework Help: Computer Sciene-java

Posted by Lisa on Sunday, November 1, 2009 at 10:35pm.

Write a loop that will continuously prompt the user for integers to sum, -1 to exit. Sum these values and display the sum to the console.

Here is what I have so far, but I feel like I am missing something:

public class Sum1 {

public static void main (String [] args) {

Scanner in = new Scanner(System.in);

final int Sentinel = -1;
int i;
int sum = 0;

System.out.println("Enter an integer:");
i = in.nextInt();

while(i != Sentinel) {

System.out.println("Enter an integer:");
i = in.nextInt();

sum = sum + i;

System.out.println("sum: " + sum + ".");

}

}

}

Answer this Question

First Name:
School Subject:
Answer:

Related Questions

Members