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

Thread: Need help with a java statement.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help with a java statement.

    Okay here is the assignment. I need to use a for loop to solve this problem.

    You are offered two salary options for ten days work. Option 1: $100 per day.
    Option 2: $1 for the first day, $2 for the second day, $4 for the third day, and so on,

    I have already figured out a way to solve the first portion of the problem. But the 2nd problem is giving me a hard time

    /**
     * 
     * 
     * CSC 225 - Online
     * Problem 8
     */
     
    import java.text.*;
     
    public class PBS5_8
    {
        public static void main(String [] args)
        {
     
            double optionOne, optionTwo, increment, sum1, sum2; 
            optionOne = 100;
            optionTwo = 1;
            sum1 = 0;
            sum2 = 0;
     
            for(increment = 1; increment <= 10;   )
            {   
                sum1 = sum1 + optionOne;
                sum2 = sum2 + optionTwo * 2;
                increment++;
            } 
     
            System.out.println(sum1);
            System.out.println(sum2);
     
     
        }
    }


    Here is my output!
    1000.0
    20.0

    Okay 20.0 should really be 1023.00 but I don't know how to make the value multiple itself and then add it to the sum. Is there any suggestions? FYI I already written the arithmetic but the coding portion is giving me a hard time.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with a java statement.

    I already written the arithmetic
    Could you post the formulas that you have found?

    Write a line for 1 day
    and another line for the 2nd day
    and another for the 3rd

    and see it what the pattern is so you can code it in a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a java statement.

    Okay well its not really a formula. It just notes I wrote down out.
    1
    2=1*2
    4=2*2
    8=4*2
    16=8*2
    etc

    The common factor is two but after that I really don't know else to really do. I played with the java for a bit but I just don't know what else to do. Any ideas?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with a java statement.

    What about the pattern? Each line takes the results (first column) from the previous line and uses it as the first operand of the expression on the current line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a java statement.

    Okay.... Yeah i see that but how should I write that in java. I mean here is what I think.

    sum2 = sum2+ OptionTwo*2;

    It goes through the for loop operation but it never produces any other output except the initial answer.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with a java statement.

    Look at the formulas in post#3. There isn't a + operator there.

    Write the same style formulas for the statement in post#5 as those in post#3 and see the difference.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a java statement.

    Well I can do that Norm. But if I do that it will just print out each individual statements instead of adding them and then printing the sum . When I was working on it I realize she wanted the total sum of the two. Not the actual print out of all the integers.

    --- Update ---

    I know Im missing something but I don't know what.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with a java statement.

    There are two parts:
    1) multiply last by 2
    2) sum the products
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a java statement.

    So I should write two parts and then display the sum. I was thinking I could put it under one statement. Is that even possible?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with a java statement.

    Take a piece of paper and work out the possibilities.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a java statement.

    okay Ill try that thanks again

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. Not A Statement - Java Error
    By shagil.a.gopinath in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2012, 04:54 PM
  4. Java If Statement
    By thretz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2012, 07:57 PM
  5. If Statement Java Need help
    By Keno888 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 25th, 2009, 01:53 AM