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 2 of 2

Thread: IF statement accumulataion not working properly - random numbers

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to use the Random statement to generate a random attack.
    By Shzylo in forum Java Theory & Questions
    Replies: 5
    Last Post: March 3rd, 2013, 07:05 PM
  2. Applet not working properly
    By kookevk in forum AWT / Java Swing
    Replies: 1
    Last Post: February 3rd, 2012, 01:29 AM
  3. Scanner not working properly.
    By kookevk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 25th, 2012, 10:14 PM
  4. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 03:17 PM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM