import java.util.*; public class UserCumulativeSum2{ public static void main(String[] args){ Scanner console = new Scanner(System.in); cummulativeSum1(console); cummulativeSum2(console); cummulativeSum3(console); } //this method finds the cummulative sum of values //the user enters (until the user enters -1) public static void cummulativeSum1(Scanner console){ int sum = 0; //ask for the first value outside the loop System.out.print("Enter value (-1 to quit): "); int userValue = console.nextInt(); while(userValue != -1){ sum += userValue; System.out.print("Enter value (-1 to quit): "); userValue = console.nextInt(); } System.out.println("Sum was: " + sum); } //this method finds the cummulative sum of values //the user enters (until the user enters -1) public static void cummulativeSum2(Scanner console){ int sum = 0; int userValue = 98723549; // some number to get us in the loop // this value will get replaced before // it gets added to the sum //loop until the user enters -1 while(userValue != -1){ System.out.print("Enter value (-1 to quit): "); userValue = console.nextInt(); if(userValue != -1){ sum += userValue; } } System.out.println("Sum was: " + sum); } //this method finds the cummulative sum of values //the user enters (until the user enters -1) public static void cummulativeSum3(Scanner console){ int sum = 0; int userValue = 0; // a number that gets us in the loop // and doesn't mess up the sum while(userValue != -1){ sum += userValue; System.out.print("Enter value (-1 to quit): "); userValue = console.nextInt(); } System.out.println("Sum was: " + sum); } }