• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

implement use of java.util.Scanner

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic