1234567891011121314151617181920212223242526272829 |
- import java.util.Scanner;
- /**
- * Created by iyasuwatts on 10/17/17.
- */
- public class Main {
-
- public static void main(String[] args){
- Scanner reader = new Scanner(System.in);
- int sum = 0;
- int n = 0;
- boolean isANumber = false;
-
- System.out.println("Input a number:");
- while (!isANumber) {
- try {
- n = Integer.parseInt(reader.nextLine());
- isANumber = true;
- } catch (Exception e) {
- System.out.println("That's not a number");
- }
- }
-
- for (int i = 1; i <= n; i++) {
- sum += i;
- }
- System.out.println("The sum from 1 to " + n + " is: " + sum);
- }
- }
|