• Post Reply Bookmark Topic Watch Topic
  • New Topic

implement use of java.util.Scanner  RSS feed

 
Lars Hansennrto
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi just started learing java and I need some help

Here is the Q:
Write a method getSum which has an integer parameter,
which calculates and returns the sum of all numbers from 1 up to and including the value of the integer parameter.
Below is an example of dialogue when running a program that calls this method:

Enter the upper limit: 100
Sum up to 100 is 5050

_______________________


this can be done with thid code:

int answer = sum(100);
System.out.println("Sum up to 100 is " + answer);
}
public static int sum(int n) {
return (n + 1) * n / 2;
}
}


But here I must change the num 100 manualy to get another answer, so I looking for a way to do this with use of java.util.Scanner so I just can write f.ex
Enter the upper limit: 350
Sum up to 350 is 61425

Cheers
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lars Hansennrto wrote:But here I must change the num 100 manualy to get another answer, so I looking for a way to do this with use of java.util.Scanner so I just can write f.ex
Enter the upper limit: 350
Sum up to 350 is 61425

I suggest you take a look at this.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!