Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- /*
- Amon Guinan
- Wednesday, January 23, 2019
- */
- // sums ints until user inputs a negative number. Outputs sum at program end.
- import java.util.Scanner;
- public class six_twentyThree {
- public static int exit(int i, int x, int z)
- {
- if (i <= 0)
- {
- if(z != 0)
- {
- System.out.println("Total sum = "+x+".");
- }
- System.out.print("Exited with "+z+" individual inputs.");
- System.exit(-1);
- }
- return i;
- }
- public static void main(String [] args)
- {
- System.out.println("Enter a starting positive integer: ");
- System.out.println("(Input a negative number (i.e. -1) to finish process.)");
- Scanner input = new Scanner(System.in);
- int i = 0;//current input
- int x = 0;//total input
- int z = 0;//Iteration Checker. Checks whether input is first input.
- do {
- i = input.nextInt();
- exit(i,x,z);
- x = i + x;
- z = z+1;
- }
- while(i >= 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement