ChaosDecides Posted January 24, 2016 Share Posted January 24, 2016 I'm currently working on a basic craps simulator that needs to ouput the total wins and losses based on x number of interations, as well as output a win percentage. I haven't added the percentage part yet. I'm running it in eclipse and sometimes I'll get the output I want and other times I'll get no output at all. This is the code I've written so far: public class craps { public static void main(String[] args) { int dieOne = (int)(Math.random()* 6) + 1; int dieTwo = (int)(Math.random()* 6) + 1; int roll = dieOne + dieTwo; int winCount = 0; int lossCount = 0; while (winCount + lossCount < 100) if (roll == 2 || roll == 3 || roll == 12){ lossCount++; } else if (roll == 7 || roll == 11){ winCount++; } else { dieOne = (int)(Math.random()* 6) + 1; dieTwo = (int)(Math.random()* 6) + 1; int roll2 = dieOne + dieTwo; while (roll2 != 7){ if(roll == roll2){ winCount++; break; } else { dieOne = (int)(Math.random()* 6) + 1; dieTwo = (int)(Math.random()* 6) + 1; roll2 = dieOne + dieTwo; } if (roll2 == 7){ lossCount++; } if (winCount + lossCount == 100){ System.out.println("Wins: " + winCount); System.out.println("Losses: " + lossCount); } } } }} I'm not seeing what part of the code might return zero ouput, unless eclipse is to blame. The second while loop is supposed to be if you roll a point and are rolling until you make the point or crap out. I'd appreciate any advice. Thanks. Link to comment Share on other sites More sharing options...
davej Posted January 24, 2016 Share Posted January 24, 2016 Your first while loop seems to be missing an opening curly brace. Have you tried single-stepping through your code? That is a good way to find logic errors. Link to comment Share on other sites More sharing options...
ChaosDecides Posted January 24, 2016 Author Share Posted January 24, 2016 (edited) Thanks for pointing that out! Fixed. The output part at the end is where I think the issue is happening. Basically once the while statement is no longer true I need the output. I don't know if the loop is checking that last if statement on each pass through. When I just do the come out roll and either win or crap out it works fine. I put in a print function for each line to see that numbers were indeed being generated. However some results just didn't look right. I'd get all 7's once followed by all 11's, then I'd have several in a row with a pretty wide range of rolls. Edited January 24, 2016 by ChaosDecides Link to comment Share on other sites More sharing options...
davej Posted January 24, 2016 Share Posted January 24, 2016 If you would carefully indent your code you might see your problem. Link to comment Share on other sites More sharing options...
thescientist Posted January 24, 2016 Share Posted January 24, 2016 moving this to the java forum Link to comment Share on other sites More sharing options...
ChaosDecides Posted January 24, 2016 Author Share Posted January 24, 2016 (edited) I went through and indented everything but I'm still not seeing the problem. I cut and pasted it from eclipse and it messed up the indentation. The original looks a lot better. I added the output for percentage and it works also. Looks like a case of hiding in plain site. My arch nemesis. Edited January 24, 2016 by ChaosDecides Link to comment Share on other sites More sharing options...
davej Posted January 24, 2016 Share Posted January 24, 2016 I don't know the rules for the game Craps. Shouldn't the value of roll change inside the while loops? As I mentioned single-stepping is usually helpful. Link to comment Share on other sites More sharing options...
thescientist Posted January 26, 2016 Share Posted January 26, 2016 what kind of output are you expecting? what kind of debugging are you doing to make sure values are what you expect them to be? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now