123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Trinh Tong
  3. * SumOfInput Lab
  4. *
  5. * Uses a while loop to continually ask the user for a valid input.
  6. * In this case, a valid input is > 0 as the lab
  7. * asks for a sum of n (userInput) from 1 to n.
  8. */
  9. import java.util.*;
  10. public class Main {
  11. public static void main(String[] args){
  12. Scanner keyboardInput = new Scanner(System.in);
  13. int sum = 0;
  14. boolean validNum = false;
  15. while (validNum == false) {
  16. System.out.println("Enter a number, any number!");
  17. int userNum = keyboardInput.nextInt();
  18. if (userNum > 0) {
  19. for (int i = 1; i <= userNum; i++) {
  20. sum += i;
  21. }
  22. System.out.println(sum);
  23. validNum = true;
  24. } else {
  25. System.out.println("Ok, not that number. (must be > 0)");
  26. }
  27. }
  28. }
  29. }