147k views
5 votes
This the code from the last post I did

This the code from the last post I did-example-1

1 Answer

2 votes

import java.util.Scanner;

public class JavaApplication86 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int x = 0, c = 0, sum = 0;

while (true){

x = scan.nextInt();

System.out.println("You entered: "+x);

if (x == -1){

break;

}

sum += x;

c++;

}

System.out.println("The sum is "+sum);

System.out.println("You entered "+c +" numbers");

}

}

This works for me. Instead of subtracting one from c and adding one to sum, I used an if statement to break away from the while loop if the entered number is -1.

User Andynil
by
5.8k points