Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with my Range Finder programme

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with my Range Finder programme

    hi, i'm trying to write a programme that reads two integers, and outputs the sum of all even numbers between that range e.g 10 14 = 36 (10+12+14)

    here is what i have so far, but i have messed up the calculation somewhere, any help would be greatly appreciated, thankyou

    import java.util.Scanner;
     
    public class Rangefinder {
     
         public static void main(String[] args) {
     
             Scanner scan = new Scanner(System.in);
     
            int num1, num2, min, max, amt = 0;
     
            System.out.println("Please enter two integers: ");
            num1 = scan.nextInt();
            num2 = scan.nextInt();
     
            min = Math.min(num1, num2);
            max = Math.max(num1, num2);
     
            for (int x = min; x <= max; x++) {
                amt = amt + x;
            }
            System.out.println(amt);
        }
    }
    Last edited by KevinWorkman; February 3rd, 2011 at 12:54 PM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Help with my Range Finder programme

    You said you wanted to only add even numbers in the range. But you're actually adding all of the numbers in the range.

    Put a print statement inside your loop so you know what's going on. Or better yet, step through the program with a debugger.

    By the way, I've edited your post to include highlight tags. Next time you post code, make sure you use them. Nobody likes reading unformatted code.

    Heh. First moderator action. Neat.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Simple Programme for Date
    By bhavinsparikh in forum Java Theory & Questions
    Replies: 1
    Last Post: August 28th, 2010, 07:24 AM
  2. [SOLVED] Help; algorithm to determine 'range'
    By b_jones10634 in forum Algorithms & Recursion
    Replies: 13
    Last Post: August 24th, 2010, 04:57 PM
  3. access cisco router with java programme
    By vigneswara in forum Java Theory & Questions
    Replies: 1
    Last Post: May 11th, 2010, 01:36 AM
  4. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 01:46 PM
  5. set the slider's models (range)
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2009, 10:59 PM