Advertisement
sudoaptinstallname

6.23

Jan 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. package com.company;
  2.  
  3. /*
  4. Amon Guinan
  5. Wednesday, January 23, 2019
  6. */
  7. // sums ints until user inputs a negative number. Outputs sum at program end.
  8.  
  9.  
  10. import java.util.Scanner;
  11.  
  12.  
  13. public class six_twentyThree {
  14.  
  15. public static int exit(int i, int x, int z)
  16. {
  17. if (i <= 0)
  18. {
  19. if(z != 0)
  20. {
  21. System.out.println("Total sum = "+x+".");
  22. }
  23. System.out.print("Exited with "+z+" individual inputs.");
  24. System.exit(-1);
  25. }
  26. return i;
  27. }
  28.  
  29. public static void main(String [] args)
  30. {
  31. System.out.println("Enter a starting positive integer: ");
  32. System.out.println("(Input a negative number (i.e. -1) to finish process.)");
  33. Scanner input = new Scanner(System.in);
  34. int i = 0;//current input
  35. int x = 0;//total input
  36. int z = 0;//Iteration Checker. Checks whether input is first input.
  37. do {
  38. i = input.nextInt();
  39. exit(i,x,z);
  40. x = i + x;
  41. z = z+1;
  42. }
  43. while(i >= 0);
  44.  
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement