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: IF statement accumulataion not working properly - random numbers

1. ## IF statement accumulataion not working properly - random numbers

I am having trouble figuring out how to write program code that will add up the number of doubles rolled, as well as the number of 7's rolled;the program already notifies the user which dice are 7's and which are doubles.

I know for the accumulation to work, it has to be an IF statement. I placed my IF statement within my MAIN method, but it does not seem to be working properly. For program running purposes only, I /* */ out the IF statement in case you wanted to copy and paste the program to see how it runs without the IF statement.

I am getting the hang of Java, but it's always tricky and confusing a bit.

```
import java.util.Random;
public class DiceGame
{
public static void main(String [] args)
{
Random newnum = new Random();

int roll_1 = 0;
int roll_2 = 0;
int counter;

boolean Doubles;
int Doublecounter;

System.out.println("Dice #1" + "\t" + "\tDice #2");

for(counter=1;counter<=10;counter++)
{
roll_1=1 + newnum.nextInt(6);
roll_2=1 + newnum.nextInt(6);

System.out.println(" ");
System.out.print(roll_1);

System.out.print("\t" + "\t" + " ");
System.out.print(roll_2);

Doubles(roll_1, roll_2);
Seven(roll_2, roll_1);

}

/*
int Doublescounter;
if(Doublecounter > 1)
{
System.out.print("Number of doubles include: ");
Doublescounter++;
}*/

}

public static boolean Doubles(int roll_1, int roll_2)
{
if(roll_1==roll_2)
{

System.out.print("     - Doubles were rolled.");
return true;
}
else
return false;

}

public static boolean Seven(int roll_1, int roll_2)
{
if((roll_1 + roll_2)==7)
{
System.out.print("     - The roll totaled 7.");

int total1 = 0;
return true;
}
else
return false;

}

}```

2. ## Re: IF statement accumulataion not working properly - random numbers

The variable that holds the count MUST be defined and initialized outside of the processing loop.