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

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a while or do loop to represent the Rice and Chessboard case

    Hi, thanks for the reply.

    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
    Last edited by WhyExist; October 11th, 2012 at 09:54 AM.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    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!

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    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!

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    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. How do I represent any character in a piece of code?
    By 93tomh in forum Java Theory & Questions
    Replies: 3
    Last Post: July 15th, 2012, 05:28 AM
  2. A chessboard with loops!
    By Hass in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 5th, 2011, 04:47 AM
  3. Create an Array wich represent Classes
    By Sjaakie91 in forum Collections and Generics
    Replies: 3
    Last Post: September 21st, 2011, 07:54 AM
  4. Chessboard filling issue
    By olemagro in forum AWT / Java Swing
    Replies: 7
    Last Post: January 23rd, 2010, 07:07 PM
  5. How to represent a chain of moves?
    By maikeru in forum Collections and Generics
    Replies: 0
    Last Post: December 31st, 2009, 01:24 AM