The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with Code

1 reply on 1 page. Most recent reply: Oct 16, 2009 6:45 AM by George B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ravi Raju

Posts: 1
Nickname: javaiscool
Registered: Oct, 2009

Help with Code Posted: Oct 15, 2009 4:01 PM
Reply to this message Reply
Advertisement
Hello everybody, sorry but I am a novice and I am not that great at programming. The program I need to create is that it adds all the even numbers from 2 inclusively until the inputed value. I am actually having problems at the for statement because I don't know how to add the integers there. Thanks for your consideration.

import java.util.Scanner;

public class Text2
{
public static void main(String args[])
{
int limit;
int y;
Scanner scan = new Scanner(System.in);
System.out.println("Please input a value greater than 2");
limit = scan.nextInt();

while (limit <= 2)
{
System.out.println("Please reenter a value greater than 2");
limit = scan.nextInt();
}
System.out.println();
System.out.println("Here is the sum");
for (int x = 2; x <= limit; x += 2)
{
System.ouut.println("I am confused what to do here");
}
System.out.println("Done");
}
}

Ravi Raju


George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Help with Code Posted: Oct 16, 2009 6:45 AM
Reply to this message Reply
    System.out.println("Here is the sum");
 
    y = 0;                  // initialize the sum variable
    for (int x = 2; x <= limit; x += 2) 
    {
        y = y + x;          // add x to it each time through the loop
    }
    System.out.println(y);  // print it out
 
    System.out.println("Done");

Flat View: This topic has 1 reply on 1 page
Topic: deploy and save EXCEL to the server Previous Topic   Next Topic Topic: Help needed turning this program into 2-class and demo.. plz help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use