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: Using a while or do loop to represent the Rice and Chessboard case

1. ## Using a while or do loop to represent the Rice and Chessboard case

The rice and chessboard case = "Oh emperor, my wishes are simple. I only wish for this. Give me one grain of rice for the first square of the chessboard, two grains for the next square, four for the next, eight for the next and so on for all 64 squares, with each square having double the number of grains as the square before."

I have to use a while or do loop to replicate this but only upto the square that will contain a 100 grains of rice. (It is the 8th square where I hit 128 grains of rice.)

I have defined an integer as 1 and I have been trying to use *2 in the while loop but it only multiplies 1 by 2 an endless number of times.

I tried this:
int i = 1;
while (i<=64) {

int l = ((i)*2);

2. ## Re: Using a while or do loop to represent the Rice and Chessboard case

You should post your code in the form of an SSCCE so we can see what's really going on. Don't forget the highlight tags.

But you're going to have to think about how you solved it in the first place- how do you know that it's the 8th square where you should stop? What did you do in your head to figure that out? Write out the steps on paper, and that will be your algorithm that you have to turn into code.

3. ## Re: Using a while or do loop to represent the Rice and Chessboard case

while (i<=64) {
only upto the square that will contain a 100 grains of rice
What does 64 have to do with a square containing 100 grains of rice?

4. ## Re: Using a while or do loop to represent the Rice and Chessboard case

I wrote the problem down on paper I can't see a correlation between the number of squares and the grains of rice. But I've noticed that the rice is multiplied by 2 each time.

Square 1 = 1 rice
Square 2 = 2 rice
Square 3 = 4 rice
Square 4 = 8 rice
Square 5 = 16 rice
Square 6 = 32 rice
Square 7 = 64 rice
Square 8 = 128 rice

I tried the following code:

public class ChessRice {
public static void main(String[] args) {
int maxSquares = 64;
int number;
number = 1;
for (int i=1; i<=maxSquares; i++) {
System.out.println((number*i)*2);
}
}
}

But it prints out:

2
4
6
8
10
12
14
16
etc

5. ## Re: Using a while or do loop to represent the Rice and Chessboard case

An error on my part it is was supposed to represent the amount of squares on a chessboard but I don't know why that was relevant.

6. ## Re: Using a while or do loop to represent the Rice and Chessboard case

Well, the only number you're incrementing is i. What does that represent? In the stuff you wrote out, where is the equivalent of i?

7. ## Re: Using a while or do loop to represent the Rice and Chessboard case

I think i is the equivalent of the grains of rice because for the next grain of rice on a square we are doing *2?

8. ## Re: Using a while or do loop to represent the Rice and Chessboard case

If i is equal to the grains of rice, and you're multiplying the grains of rice by 2 each time, what is the significance of adding 1 to i each time?

9. ## Re: Using a while or do loop to represent the Rice and Chessboard case

If you want to loop until you have 100 grains of rice on a square, the conditional for the loop should include '100' and the 'number-of-grains-on-the-current-square' in question. This way if the requirements changed later on, you could adjust your code as easily as changing the 100 to a new value.
I also would like to mention that the 64 in the posted code is considered a magical number and should be replaced with a variable. Except that it should be a number other than 64 too. A variable like maxGrainsPerSquare or something with meaning...

10. ## Re: Using a while or do loop to represent the Rice and Chessboard case

I see. I must have read or misunderstood my textbook for some reason I thought i++ meant it displays the numbers in ascending order and i-- in descending order. What sorts of things can the last bit of for do?

for (int i = 1; i <= 100; i= i*2)

That is what I need but I just need to convert it to a while loop.

Thanks for the help.

11. ## Re: Using a while or do loop to represent the Rice and Chessboard case

Either way, I think you're trying to do too much inside the loop statement. Think about how you might use other variables instead of trying to do everything with a single variable i.