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

Thread: Need loop idea please!

  1. #1
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool Need loop idea please!

    I'm trying to create a method called myOddNum(int m, int n)

    The method needs to obtain 2 odd integers from the user (ex: m = 5 and n = 9)
    and it needs to calculate the following series: 5 - 7 + 9 = 7
    Which is the same as (m - (m + 2) + (n -2) + n)

    I'd like to find a way to use a loop to calculate this method. Any ideas?

    Thank you!!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Need loop idea please!

    Do you have any ideas? What have you tried? Where are you stuck?

    How would you do this in your head, without a computer?

    Recommended reading: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Actually, why do you want to do this with a loop? You seem to already have the math figured out. Can you give an example with a larger range?
    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!

  3. #3
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need loop idea please!

    I know what I need the loop to do, I just don't know how to execute it in the body of the loop. I'm very new to java and need to do this for a homework assignment. Let's use another example - m= 7 and n = 101

    Here is what I have so far: (I know the loop will not break the way it is written)

    import javax.swing.JOptionPane;


    public class OddSum {


    public static void main(String[] args) {

    int m,n;

    String num1Str, num2Str;

    //Get a pair of integers from users
    num1Str = JOptionPane.showInputDialog("Enter a positive odd integer: ");
    m = Integer.parseInt(num1Str);

    num2Str = JOptionPane.showInputDialog("Enter a positive odd integer that is larger than " + m );
    n = Integer.parseInt(num2Str);


    System.out.println (myOddNum(m,n));

    }

    public static int myOddNum(int m, int n){

    int localSum = 0;
    int temp = 0;

    while (m <=n){
    temp = m - (m + 2) + (n - 2) + n;
    localSum = localSum + temp;


    }
    return localSum ;

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Need loop idea please!

    What should the answer be when m is 7 and n is 101?
    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!

  5. #5
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need loop idea please!

    7 - 9 + 11 - 13 + 15 - 17 + 19 - 21 + 23 - 25 + 27 - 29 + 31 - 33 + 35 - 37 + 39 - 41 + 43 - 45 + 47 - 49 + 51 - 53 + 55 - 57 + 59 - 61 + 63 - 65 + 67 - 69 + 71 - 73 + 75 - 77 + 79 - 81 + 83 - 85 + 87 - 89 + 91 - 93 + 95 - 97 + 99 - 101 = -48

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need loop idea please!

    Quote Originally Posted by Montrell79 View Post
    needs to calculate the following series: 5 - 7 + 9 = 7
    Which is the same as (m - (m + 2) + (n -2) + n)
    Erm.. no it isn't

    M - (M + 2) + (N - 2) + N == 5 - 7 + 7 + 9
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need loop idea please!

    I think I'm a little closer now, but this method only works using the example given of m =5 and n =9, but not if I plug in other numbers that are more than 4 digits apart. I know there is an error in my logic. Can anyone help? Thanks!!


    while (m <=n){

    temp = m *(Math.pow(1, -3)) - 2;

    m+= 2;

    localSum = (int) temp;


    Sorry but I don't know what this means: Please use
    //code goes here...
    tags when posting your code

  8. #8
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need loop idea please!

    Let's try this again:

    //
    while (m <=n){
     
     temp = m *(Math.pow(1, -3)) - 2;
     
     m+= 2;
     
     localSum = (int) temp;

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Need loop idea please!

    theFuture, please read this before you post again: The Problem with Spoon-feeding
    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. A Program Idea
    By drbacchoi in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2012, 09:15 AM
  2. No idea how to fix it.
    By Hammer67 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 13th, 2011, 10:41 AM
  3. need help with my idea!
    By frostwing in forum Object Oriented Programming
    Replies: 3
    Last Post: February 10th, 2011, 08:53 AM
  4. Looking for algorithm idea help
    By Maineiac in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 18th, 2010, 06:38 PM
  5. No idea what to do for this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2010, 07:16 PM