Results 1 to 2 of 2

Thread: Simple Help....

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

    Default Simple Help....

    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();
    
         }
    
         for (i = first; i <= last; i++)
         {
           total = total + i;
         }
     
         System.out.println("The sum of the numbers are " + total);
       }
    }
    <b>What I need to do is create a user defined method to solve the problem. As you can see I've already done the program, but how would i go about inserting a user defined method?. What would I need to do, and where do i need to close the main method in order to start the user defined method?</b>

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Post write code

    Java Code:
    public class Summer
    {
       // Insert a user defined method.
       int userDefinedMethod( int startValue)
       {
            return ++startValue;
       }
       public static void main (String[] args)
       {
         [B]Summer summer = new Summer();//[/B]  
         int first = 0;
         int last = 100;
         // feedback of some kind.
         System.out.println("Hello. I will advance counter.");
         // Oversimplified.
         while (++first < last)
         {
               [B]System.out.println(summer.userDefinedMethod(first));[/B]
         }
           System.out.println("done");
       }
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Simple Form
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:38 PM
  2. Simple Table Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:58 PM
  3. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  4. A Simple Web Server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:04 PM
  5. simple GUI
    By dim_ath in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 03:00 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
  •