Results 1 to 9 of 9
Thread: What did i do wrong
- 11-14-2008, 07:30 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 19
- Rep Power
- 0
What did i do wrong
after inserting my new method, it doesnt solve the problem. It still goes through and if i put in a larger starting number with a smaller ending number it tells me to try again, but it never adds up the numbers.Java Code:import java.util.*; public class Summer { public static void main (String[] args) { Scanner in = new Scanner(System.in); int first = 0; int last = 0; int total = 0; int i = 0; System.out.println("Hello. I will calculate the sum"); System.out.println("of consecutive integers."); System.out.println("Please enter the starting number:"); first = in.nextInt(); System.out.println("Please enter the ending number:"); last = in.nextInt(); while (last < first) { System.out.println(last + " is smaller than " + first); System.out.println("The ending number must be bigger than the first"); System.out.println("Enter the starting number again"); first = in.nextInt(); System.out.println("Enter the ending number again"); last = in.nextInt(); System.out.println("The sum of the numbers are " + total); total = sum(last, first); } for (i = first; i <= last; i++) { total = total + i; } } public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } }
- 11-14-2008, 07:43 PM #2
Java Code:import java.util.*; public class Summer { public int sum(int num1, int num2) { int result; result = num1 + num2; return result; } public static void main (String[] args) { Scanner in = new Scanner(System.in); int first = 0; int last = 0; int total = 0; int i = 0; System.out.println("Hello. I will calculate the sum"); System.out.println("of consecutive integers."); System.out.println("Please enter the starting number:"); first = in.nextInt(); System.out.println("Please enter the ending number:"); last = in.nextInt(); while (last < first) { System.out.println(last + " is smaller than " + first); System.out.println("The ending number must be bigger than the first"); System.out.println("Enter the starting number again"); first = in.nextInt(); System.out.println("Enter the ending number again"); last = in.nextInt(); System.out.println("The sum of the numbers are " + total); } total =new Summer().sum(last, first); for (i = first; i <= last; i++) { total = total + i; } System.out.println(total); } }
- 11-15-2008, 03:38 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually no need to use method sum, for loop do the calculation for you.
Remove this line from the serjants' code above and try.Java Code:total =new Summer().sum(last, first);
- 11-15-2008, 10:10 AM #4
- 11-17-2008, 04:27 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, and still no response at all here. I think he wants to find the sum of all numbers within two integers, need to validate on that second number should be greater that the first.
- 11-17-2008, 09:23 AM #6
I think again somebody wrote the code for him and he jumps from forum to forum with the code.
- 11-17-2008, 09:27 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you comes with cross-posting in another forum.
- 11-17-2008, 10:01 AM #8
In another thread you will see that he even doesn't know about main method,look at the "i will pay" thread and you will understand.
- 11-17-2008, 10:07 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes lol. I go through that thread few minutes back. Only thing we can do is wait and see. ;)
Similar Threads
-
what's wrong in here!!!
By Annatar in forum New To JavaReplies: 8Last Post: 11-14-2008, 02:55 AM -
So, what am I doing wrong?
By Charles_Smith in forum New To JavaReplies: 0Last Post: 10-29-2008, 02:50 PM -
right or wrong
By jot321 in forum New To JavaReplies: 7Last Post: 09-25-2008, 11:45 AM -
Can someone tell me what I did wrong??
By booter4429 in forum New To JavaReplies: 7Last Post: 08-13-2008, 08:35 PM -
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks