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

Thread: need help with this loop

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

    Default need help with this loop

    Im very new at java and this is the last thing i have to finish before get some sleep. I just cant seem to finish the program. The goal is to make a program where the user inputs a number, then recieves the limit, range and sum of the odd numbers that make up the entered number. I have most of it done, i just cant seem to get the program to print the total sum of the odd numbers

     import java.util.Scanner;
     
    class Sumoddjkunitz {
    	public static void main(String[] args)
    			{
           int user1, div;
     
    //This tells the user what to do
    System.out.println("This program will tell you the sum, limit, and range of odd numbers between 1 and your chosen number");
    System.out.println("Please enter the limit number");
     
      Scanner keyboard = new Scanner(System.in);
      user1 = keyboard.nextInt();
     
    //spacer below
     System.out.println("   ");
     
    //limit
    System.out.print("The Limit of the numbers is ");
    System.out.print(user1);
     
    //spacer below
     System.out.println("   ");
     
    //range
    System.out.print("The range of the numbers is ");
    System.out.print("1 to ");
    System.out.print(user1);
     
    //spacer below
     System.out.println("   ");
     
    //sum of odd numbers and loop
    System.out.print("The sum of the numbers is ");
     
    sum = 0;
    for (int j = 1; j <= user1; j++) {
        if (j % 2 == 1) {
            sum += i;
        }
    }
     
     
           }
    			}
    Last edited by musterdplug12; December 4th, 2011 at 08:21 PM.


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

    Default Re: need help with this loop

    i never used the div integer, forgot to take it out

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: need help with this loop

    You calculate the sum but where do you display it?

    By the way, you can make your code more efficient if you change j++ to j += 2 and get rid of the if statement.
    Improving the world one idiot at a time!

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

    Default Re: need help with this loop

    ok so im a day late but ive been busy with work. I now show the sum but its not coming out correctly, whats wrong with my code? Please dont trash me about this, im just learning java.

      import java.util.Scanner;
     
    class Sumoddjkunitz {
    	public static void main(String[] args)
    			{
           int user1, i, sum;
     
    //This tells the user what to do
    System.out.println("This program will tell you the sum, limit, and range of odd numbers between 1 and your chosen number");
    System.out.println("Please enter the limit number");
     
      Scanner keyboard = new Scanner(System.in);
      user1 = keyboard.nextInt();
     
    //spacer below
     System.out.println("   ");
     
    //limit
    System.out.print("The Limit of the numbers is ");
    System.out.print(user1);
     
    //spacer below
     System.out.println("   ");
     
    //range
    System.out.print("The range of the numbers is ");
    System.out.print("1 to ");
    System.out.print(user1);
     
    //spacer below
     System.out.println("   ");
     
    //sum of odd numbers and loop
    System.out.print("The sum of the numbers is ");
     
    sum = 0;
    for (int j = 1; j <= user1; j++) {
        if (j % 2 == 1) {
            sum += j;
    System.out.print(sum);
        }
    }
     
     
           }
    			}

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

    Default Re: need help with this loop

    nm, figured it out, thanks to the community for existing though!

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: need help with this loop

    Quote Originally Posted by Junky View Post
    By the way, you can make your code more efficient if you change j++ to j += 2 and get rid of the if statement.
    <sigh>

    Good advice gets ignored again.
    Improving the world one idiot at a time!

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  3. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM