Help with this loop/program!!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Help with this loop/program!!!

  1. #1
    Join Date
    Sep 2004
    Posts
    37

    Help with this loop/program!!!

    Hello all, this site was suggested to me by a fellow student and I am hoping to get some help. Have just begun an introduction to Java course and am finding it quite difficult.

    In this program I have to roll a dice twice, add up the numbers and report when two ones come up (Snakeyes) and how many rolls it took. I am having trouble with which loop to use and how to count the number of rolls that have been executed before "snakeyes comes up".

    Here is what I have done:class SnakeEyes
    {
    public static void main(String[] args)
    {
    public static int roll()
    {
    int total = 0;
    int dice1 = (int)(Math.random()*6)+1;
    int dice2 = (int)(Math.random()*6)+1;

    total = dice1 + dice2;
    return total;
    }

    do
    {
    for( int count == 0; count++);
    roll;
    if (total==2) break;
    count++;
    }
    while (total != 2);


    System.out.println("You have rolled Snake Eyes!");
    System.out.println("This took" +count "rolls");

    }
    }

    If anyone has any suggestions, that would be great! Thankyou.
    Steve.

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Well the biggest mistake you've made that I can see is that you haven't declared the methods correctly. You cannot declare one method inside a different method. At the minute, you have the method roll() declared inside the main() method. This will fail to compile. You need to alter it so that all methods are declared seperately at the class level.

    The method roll() itself looks ok, it should work fine. You have some logic errors in the for loop though, it won't do what you expect it to.

    Code:
    for( int count == 0; count++);
    The first point to note about the for loop is that you've missed a section out. For loops have three sections and look as follows
    Code:
    for(statement;boolean expression; statement)
    It must have all three of those sections to compile, and you only have two. You need to add all three sections.

    The first statement is only run once, just before the loop starts. It is generally used to create a variable to control the loop. The boolean expression is run just before each itteration of the loop. If it evaluates to TRUE then the loop runs, if it evaluates to FALSE the loop terminates. The last statement is run at the end of each itteration. It is generally used to increment the control variable that was created in the first statement.

    Another mistake is the semicolon you have at the end of the for loop declaration. It will compile correctly with that semi-colon there, but it won't do what you expect. It will just mean that the for loop does absolutely nothing. The code that the for loop runs is contained within a seperate scope to the rest of the code. Put simply, this means that you need opening and closing curly brackets after the for loop declaration, and all the code that is within the for loop should be in this.

    The simplest possible for loop would look like this
    Code:
    for(;;)
    {
    }

  3. #3
    Join Date
    Sep 2004
    Posts
    37
    Thanks Mike. I think I have sorted out the methods. I am a bit stuck on the for loop! I need the method to count how many times the function roll takes place till "Snake Eyes" (rolls two 1's) comes up. Am I right in having the variable total in the for line? And also is it correct just to call the method "roll' as I have. I am getting a few errors in regards to this so if you can help out again, or anyone else that would be fantastic. Thanks.
    Here is my code:class SnakeEyes
    {
    public static int roll()
    {
    int total = 0;
    int dice1 = (int)(Math.random()*6)+1;
    int dice2 = (int)(Math.random()*6)+1;

    total = dice1 + dice2;
    return total;
    }

    public static void main(String[] args)
    {
    for(int i==0;total!=2;i++)
    SnakeEyes.roll;


    System.out.println("You have rolled Snake Eyes!");
    System.out.println("This took" +i "rolls");

    }
    }

  4. #4
    Join Date
    Jul 2004
    Posts
    83
    import java.util.Random;
    public class SnakeEyes
    {
    public static void main(String[] args)
    {
    int a=0,b=0,counter=0;
    Random rand = new Random();
    while(a!=1&&b!=1)
    {
    a=rand.nextInt(6)+1;
    b=rand.nextInt(6)+1;
    counter++;
    }
    System.out.println("You have rolled snake eyes");
    System.out.println("It took " +counter+ " tries");
    }
    }
    They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    the problem with calling the method roll was that you missed the brackets out. When you call a method you need to put opening and closing brackets after the method name. If the method doesn't take any parameters then thats all you need. If the method takes parameters then you put the parameters inside the brackets.

    Code:
     roll();

  6. #6
    Join Date
    Sep 2004
    Posts
    37

    begcanoc

    Thankyou very much SportsDude and Mike! Both your suggestions were very helpful. Am starting to get the hang of this! Thankyou once again, both more helpful than my teacher.

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center