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

Thread: Help with if, else if and else statements

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with if, else if and else statements

    When I run this code for the quad, (as stated in the comments) it prints out
    “Quad- Congrats, you win!” and it also prints out
    "Junker- Sorry, you lose!”

    It has this same problem for the triple, where it will print the triple statement and also the junker statement after it.

    However, for the two-pair, it only prints "Two-Pair- Congrats you win!” and no junker, which is what I want.

    So my question is that why does it still print the junker line after the “congrats you win” even though the junker line is in an else statement? It should only be executing the else if none of the other statements are true.


     
    import java.util.Scanner;
    public class ProblemwithProgram06
    {
    public static void main(String []args)
    {
    	Scanner stdIn= new Scanner(System.in);
    	int number;
    	int reroll=0;
    	int wins=0;
    	int loses=0;
    	int rounds=0;
    	String play;
    	int d1, d2, d3, d4;
     
    	// Instructions for game
    	System.out.println("    Welcome to Computer Dice    ");
    	System.out.println("---------------------------------");
    	System.out.println("You will first roll your dice \n");
    	System.out.println("You are then allowed to re-roll up to \ntwo of your dice \n");
    	System.out.println("Finally the outcome of your roll is \nto be determined: \n");
    	System.out.println("Any Quad and you receive 108 Wins");
    	System.out.println("Any Triple and you receive 6 Wins");
    	System.out.println("Any Two-Pair and you receive 4 Wins");
    	System.out.println("Anything else and you receive 1 Lose");
    	System.out.println("-------------------------------------\n \n");
     
    	do //beginning of do loop
    	{
    		System.out.println("  Player  ");
    		System.out.println("----------");
    		d1 = (int)(Math.random() * 6) + 1;
    		d2 = (int)(Math.random() * 6) + 1;
    		d3 = (int)(Math.random() * 6) + 1;
    		d4 = (int)(Math.random() * 6) + 1; 
    		System.out.println(" " + d1 + " " + d2 + " " + d3 + " " + d4 + "\n"); 
     
    		do 
    		{
    			System.out.print("Please enter the number of dice to re-roll [0,2] :  ");
    			number=stdIn.nextInt();
    			System.out.print("\n");
    		} while (number >2 || number <0);
     
     
    		for (int i=1; i <= number; ++i)
    		{ //beginning of for loop
    			System.out.print("");
     
    			do
    			{
    				System.out.print("Please enter the number of the die to re-roll [1,4] : ");
    				reroll=stdIn.nextInt();
    				System.out.print("\n");
    			} while (!(reroll >= 1 && reroll <= 4));
     
    			if (reroll == 1)
    			{
    				d1 = (int)(Math.random() * 6) + 1;
    			}
     
    			if (reroll == 2)
    			{
     
    				d2 = (int)(Math.random() * 6) + 1;
    			}
     
    			if (reroll == 3)
    			{
     
    				d3 = (int)(Math.random() * 6) + 1;
    			}
     
    			if (reroll == 4)
    			{
     
    				d4 = (int)(Math.random() * 6) + 1;
    			}
    		} //end of for loop
     
    		do 
    		{
    		System.out.println("  Player  ");
    		System.out.println("----------");
    		System.out.println(" " + d1 + " " + d2 + " " + d3 + " " + d4 + "\n");
    		break;
    		} while (number >0);
     
     
     
     
     
    		//****************************************************
     
     
    		//This is the start of my problem
    		//Quad
    		if (d1==d2 && d2==d3 && d3==d4)
    		{
    			System.out.println("Quad- Congrats, you win! \n");
    			wins= wins +108;
    		}
     
    		// Two-Pair 
    		else if ((d1==d2 && d2!=d3 && d3==d4) || (d1==d3 && d3!=d2 && d2==d4) || (d1==d4 && d4!=d3 && d3==d2))
    		{
    			System.out.println("Two-Pair- Congrats you win! \n");
    			wins= wins +4;
    		}
     
    		//Triple
    		else if ((d1==d2 && d2==d3 && d4!=d1) || (d1==d2 && d2==d4 && d3!=d2) || (d1==d3 && d3==d4 && d2!=d3) || 
    				(d2==d3 && d3==d4 && d1!=d4))
    		{
    			System.out.println("Triple- Congrats you win! \n");
    			wins= wins +6;
    		}
     
    		//Junker
    		else
    		{
    			System.out.println("Junker- Sorry, you lose! \n");
    			loses= loses +1; 
    		} // This is the end of the problem
    		//*****************************************
     
     
     
     
    		do
    		{
    			System.out.print("Do you wish to play again [y, n] : "); 
    			play=stdIn.next();
    		} while (!play.equals("y") && (!play.equals("n")));
     
    		++rounds;
    		System.out.print("\n \n");
     
    	} while (play.equals ("y")); //end of do loop 
     
    	System.out.println("Computer Dice Results");	
    	System.out.println("-----------------------");
    	System.out.println("You played " + rounds + " rounds");		
    	System.out.println("Wins: " + wins);
    	System.out.println("Loses: " + loses);
    	System.out.println("-----------------------");
     
    	stdIn.close();
    	}	
    }
    Last edited by kaitlynn_rossee; March 24th, 2018 at 07:17 PM. Reason: Added code tags and indentation

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    The indentations of the code makes the logic hard to follow. The nested do{} while statements need to be indented.
    Please fix the indentations to show the logic.

    Can you copy the contents of the console and paste it here? Add some comments where there is a problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with if, else if and else statements

    Just to let you know, I fixed the code so hopefully it will work now. If there's any other problems with it please feel free to let me know. Thanks.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    The code tags I fixed have been lost.
    The code's indentations are not correct. The do{}while within another do{}while should be indented.
    	do //beginning of do loop
    	{
    		System.out.println("  Player  ");
    		System.out.println("----------");
    		d1 = (int)(Math.random() * 6) + 1;
    		d2 = (int)(Math.random() * 6) + 1;
    		d3 = (int)(Math.random() * 6) + 1;
    		d4 = (int)(Math.random() * 6) + 1; 
    		System.out.println(" " + d1 + " " + d2 + " " + d3 + " " + d4 + "\n"); 
     
    	       do    //<<<<<< this should be indented
    	       {
    		 System.out.print("Please enter the number of dice to re-roll [0,2] :  ");
    		 number=stdIn.nextInt();
    		 System.out.print("\n");
    	      } while (number >2 || number <0);


    Can you copy the FULL contents of the console that shows the problem and paste it here? Add some comments where there is a problem
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with if, else if and else statements

    Sorry, the forum instructions say to put the code=java only for the problem area, which is what I had done. I did not know without it, all indents go away. I have since fixed the original post. Your help is appreciated.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    That looks a lot better.

    Can you copy the FULL contents of the console from running the program that shows the problem and paste it here? Add some comments where there is a problem
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with if, else if and else statements

    Welcome to Computer Dice
    ---------------------------------
    You will first roll your dice

    You are then allowed to re-roll up to
    two of your dice

    Finally the outcome of your roll is
    to be determined:

    Any Quad and you receive 108 Wins
    Any Triple and you receive 6 Wins
    Any Two-Pair and you receive 4 Wins
    Anything else and you receive 1 Lose
    -------------------------------------


    Player
    ----------
    6 6 6 1

    Please enter the number of dice to re-roll [0,2] : 0

    Player
    ----------
    6 6 6 1

    Triple- Congrats you win!

    // This shouldn't be printing along with the triple
    Junker- Sorry, you lose!

    Do you wish to play again [y, n] :
    Last edited by kaitlynn_rossee; March 25th, 2018 at 12:08 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    Here is what I get when I run the code:
    Running: java.exe -client ProblemwithProgram06

    Welcome to Computer Dice
    ---------------------------------
    You will first roll your dice

    You are then allowed to re-roll up to
    two of your dice

    Finally the outcome of your roll is
    to be determined:

    Any Quad and you receive 108 Wins
    Any Triple and you receive 6 Wins
    Any Two-Pair and you receive 4 Wins
    Anything else and you receive 1 Lose
    -------------------------------------


    Player
    ----------
    6 6 6 1

    Please enter the number of dice to re-roll [0,2] : 0
    Player
    ----------
    6 6 6 1

    Triple- Congrats you win!

    Do you wish to play again [y, n] : n

    Computer Dice Results
    -----------------------
    You played 1 rounds
    Wins: 6
    Loses: 0
    -----------------------

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with if, else if and else statements

    Okay thanks, what IDE are you using? I'm using eclipse neon.3 and it doesn't work for me.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    I do not use an IDE. I use command prompt with javac.exe and java.exe.

    I didn't run the program as you wrote it. I changed these lines so I did not have to respond to the prompts and so the dx values would be the same as yours:
    	Scanner stdIn= new Scanner("0 n"); //System.in);  <<< provide input for testing
    ...
    		d1 = 6;//(int)(Math.random() * 6) + 1;
    		d2 = 6;//(int)(Math.random() * 6) + 1;
    		d3 = 6;//(int)(Math.random() * 6) + 1;
    		d4 = 1;//(int)(Math.random() * 6) + 1;
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with if, else if and else statements

    So how do you expect to duplicate the error if you don't run the program as is? When you change the program you aren't running my program with the errors, hence why you aren't getting any errors. Since you aren't able to duplicate my problem maybe somebody else can.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    I doubt my changes would effect the execution of the if/else if statements you are concerned about.

    One problem with your original program is getting the dice to be 6 6 6 1. How many times do you execute the program to get that results?
    How often do you get the "Junker-..." output?

    Try making the changes I did and see what the program does in your IDE.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Help with if, else if and else statements

    Quote Originally Posted by Norm View Post
    I doubt my changes would effect the execution of the if/else if statements you are concerned about.
    I agree with Norm. That's how I would test the issue too, otherwise you'd spend too much time testing and you may never reproduce the specific circumstances where the error occurs.

    I tested using Eclipse (Oxygen.2 Release (4.7.2)).

    I tested the code as is but never won at all. So I hard coded the specific situation where you saw the error and I get the same as Norm... no errors. The "if" statement behaves as expected for each condition: triple, quad, two-pair and junker.

  14. The Following User Says Thank You to User2009 For This Useful Post:

    Norm (March 26th, 2018)

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with if, else if and else statements

    Are you working with code that is the same as in post#1?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Questions about if statements and if-else statements
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 23rd, 2014, 05:37 PM
  2. Help my if statements!
    By l2code in forum Loops & Control Statements
    Replies: 1
    Last Post: March 22nd, 2013, 01:47 PM
  3. [SOLVED] If and else if statements???
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 5th, 2013, 01:44 AM
  4. If Statements help
    By cowsquad in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2013, 06:41 AM
  5. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM

Tags for this Thread