Results 1 to 9 of 9
  1. #1
    jpnym15 is offline Member
    Join Date
    Nov 2007
    Posts
    19
    Rep Power
    0

    Default What did i do wrong

    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;
                  }
    }
    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.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    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);
       }
                 
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Actually no need to use method sum, for loop do the calculation for you.

    Java Code:
    total =new Summer().sum(last, first);
    Remove this line from the serjants' code above and try.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    Quote Originally Posted by Eranga View Post
    Actually no need to use method sum, for loop do the calculation for you.

    Java Code:
    total =new Summer().sum(last, first);
    Remove this line from the serjants' code above and try.
    He didn't explain clearly what he really needed to do ;):o

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    I think again somebody wrote the code for him and he jumps from forum to forum with the code.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

  8. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    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.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 03:55 AM
  2. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 03:50 PM
  3. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 12:45 PM
  4. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •