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 3 of 3

Thread: 'for' loop problem

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 'for' loop problem

    i need to create a progam that enters even integers until the input number is reached. for some reason i think my for statement(for (int i = 2; i < x; i+= 2)) is messed up because it returns 18 intstead of 20 when 10 is entered. ive played around with this program to try to get it to work for several hours and i just can't find the solution. any pointers?
    import java.util.Scanner;
    public class EvenSum {
     
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		double x, sum = 0;
    		System.out.println("Enter number: ");
    		x = scan.nextInt();
     
    		for (int i = 2; i < x; i+= 2){ 
    			sum = i + x;		
    	}
     
    		if(x < 2){
    			System.out.println("Cannot enter number less than 2.");
    			System.exit(0);}
     
    		System.out.println("Sum of all evens is: " + sum);
    	}
    }
    thanks in advance


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 'for' loop problem

    you got it

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 'for' loop problem

    Quote Originally Posted by javaiscool View Post
    You "got dat dough" yet?
    nope

Similar Threads

  1. Help with loop problem
    By miller000 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 4th, 2013, 12:04 PM
  2. Need help with loop problem
    By sean12111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 24th, 2013, 08:39 PM
  3. [SOLVED] Loop Problem
    By Wwong3333 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2012, 03:41 PM
  4. [SOLVED] Problem with loop?
    By Rilstin81 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 23rd, 2011, 07:48 PM
  5. [SOLVED] for loop problem
    By javaneedhelp in forum Loops & Control Statements
    Replies: 3
    Last Post: October 9th, 2011, 10:25 AM