12345678910111213141516171819202122232425262728293031
  1. import java.util.Scanner;
  2. /**
  3. * Sum of the Numbers
  4. */
  5. public class Main {
  6. /**
  7. * Prompts user to enter an integer. Then calculates the sum of numbers from 1 to entered value.
  8. */
  9. public int getNumber(){
  10. Scanner scanner = new Scanner(System.in);
  11. System.out.println("Enter an integer."); //Prompt
  12. int input = scanner.nextInt(); //User supplies a value which is assigned to 'input'
  13. return input;
  14. }
  15. public int sumNumbers(int n){
  16. int x = 0;
  17. //Loop runs n times
  18. for (int i = 0; i <= n; i++){
  19. x += i; //adds current value of x to i and sets that as the new x
  20. }
  21. return x;
  22. }
  23. public void solutionBasedOnInput(){
  24. int answer = sumNumbers(getNumber()); //runs sumNumbers() with the number supplied by the user from getNumber()
  25. System.out.print(answer); //Prints the solution
  26. }
  27. }