Main.java 732B

1234567891011121314151617181920212223242526272829
  1. import java.util.Scanner;
  2. /**
  3. * Created by iyasuwatts on 10/17/17.
  4. */
  5. public class Main {
  6. public static void main(String[] args){
  7. Scanner reader = new Scanner(System.in);
  8. int sum = 0;
  9. int n = 0;
  10. boolean isANumber = false;
  11. System.out.println("Input a number:");
  12. while (!isANumber) {
  13. try {
  14. n = Integer.parseInt(reader.nextLine());
  15. isANumber = true;
  16. } catch (Exception e) {
  17. System.out.println("That's not a number");
  18. }
  19. }
  20. for (int i = 1; i <= n; i++) {
  21. sum += i;
  22. }
  23. System.out.println("The sum from 1 to " + n + " is: " + sum);
  24. }
  25. }