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

# Thread: Need help with a java statement.

1. ## 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. ## Re: Need help with a java statement.

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.

3. ## 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. ## 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.

5. ## 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. ## 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.

7. ## 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. ## Re: Need help with a java statement.

There are two parts:
1) multiply last by 2
2) sum the products

9. ## 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. ## Re: Need help with a java statement.

Take a piece of paper and work out the possibilities.

11. ## Re: Need help with a java statement.

okay Ill try that thanks again