Logic error(I cant make it do what i want) plz help

im writing a program for my lab at school and i cant figure out how to make it work. i thought i had figured it out, its close but not all the way there. im supposed to write a program in which 2 dice are rolled and the sum is added up. the dice should continue to be rolled until i get all possible sums ( 2 to 12) and it should display how many tries it took. this whole process should be repeated as many times as the user wants to play. it works fine the 1st tiime but after that it keeps printing out 0. anywhere heres how i have it written.public void rollForSum(int trials) {
        sum = value1 + value2;
        counter = 2;
        counter2 = 0;
        counter3 = 1;
        while (counter3 <= trials) {
            while (counter <= 12) {
                while (sum != counter){
                    value1 = 1 + generator.nextInt(6);
                    value2 = 1 + generator.nextInt(6);
                    sum = value1 + value2;
                    counter2++;
            counter++;
        System.out.println("It took " + counter2 + " times");
        counter2 = 0;
        counter3++;
}

Well I can see you've had a go, so good on you, but there are several problems.
You seem to be doing your counter incrementing in the wrong places, and in any case, you'd be better to use "for" loops for the fixed loops because they make the functionality clearer - something like this:public void rollForSum(int trials) {
        int tries = 0;
        for (int trial = 0 ;  trial < trials ; trial++) {
             for (int diceTot = 2 ; diceTot <= 12 ; diceTot++) {
                 do {
                    value1 = 1 + generator.nextInt(6);
                    value2 = 1 + generator.nextInt(6);
                     sum = value1 + value2;
                     tries++;
                 } while (sum != diceTot);
             System.out.println("It took " + tries + " times");
}However, I don't think this actually does what you want, because you are counting the number of tries to get a 2, then the number of tries to get a 3 and then the number of tries to get a 4, and so on up to 12. I suspect what you really should be doing is count the number of tries to get at least one of each number. To achieve that, you need to hold a flag saying whether you have found each number separately, and a counter of the numbers found so far.
I would do this by holding an array of booleans and a counter then you only need two loops, one "for" loop for trials, and an inner "while" loop (like you used) like this:int tries = 0;
for (int trial = 0 ;  trial < trials ; trial++) {
  boolean[] sums = new boolean[13];
  int sumsFound = 0;
  while (sumsFound < 11) {
    value1 = 1 + generator.nextInt(6);
    value2 = 1 + generator.nextInt(6);
    sum = value1 + value2;
    tries++;
    if (!sums[sum]) {
      sums[sum] = true;
      sumsFound++;
}Hope this helps!
Regards,
Tim

Similar Messages

  • When i try to buy 50k Coins in Jet Pack Joyride (3.99$) it says "Error al comprar" (Im from Costa Rica but my account is from USA) IDK what to do plz help me

    When i try to buy 50k Coins in Jet Pack Joyride (3.99$) it says "Error al comprar" (Im from Costa Rica but my account is from USA) IDK what to do plz help me.

    Hello Dovahkiin03,
    Thanks for using Apple Support Communities.
    For more information on this, take a look at this article:
    iTunes Store: About In-App Purchases
    http://support.apple.com/kb/ht4009
    Best of luck,
    Mario

  • I forget icloud password. Apple ID and icloud have different password  And i cant use apple id in i cloud   Plz help me

    This is my icloud acount ******
    I forget icloud password
    Apple ID and icloud have different password
    And i cant use apple id in i cloud
    Plz help me
    <Email Edited By Host>

    Hello, Alialyousufi. 
    Thank you for visiting Apple Support Communities.
    You can reset your Apple ID password via the steps in the first article below.  If you do not have access to the email account or are not receiving the password reset link, see the second article.
    Apple ID: If you forget your password
    http://support.apple.com/kb/ht5787
    Apple ID: Contacting Apple for help with Apple ID account security
    http://support.apple.com/kb/HT5699
    Cheers,
    Jason H.

  • Error 11556 cant make a ringtone from apurchased track

    This error message kees coming up when i try to make a ringtone for my iphone, from a track inmy library. it reads 'itunes could not connect ti itunes store, unkown error occured. (11556)
    It keeps doing this.
    Please help, thankyou

    Hi,
    Your profile shows that you are located in England.
    +You can only purchase ringtones from the U.S. iTunes Store. You can use GarageBand anywhere to export your original song, your original audio recordings, or use Apple Loops and iLife jingles to create a custom ringtone for your iPhone.+
    from http://support.apple.com/kb/HT1398
    -Jason

  • I restored my Macbook Pro and now I can't log in as Admin - it says there is an error with FileVault and I don't have access - plz help!  thank you.

    I had to restore my Macbook Pro from Time Capsule yesterday and now when I go to log in as my usual Admin, it says there is a FileVault error and my access is denied.  I created another temporary Admin acct, but everything I had on my desktop, etc is "gone"  I can look up the "drive' or wahtever it's called, but I can't access any of it.  All I want to do is log back in as my usual self (the regular Admin) - plz help!  Thanks.   p.s. I"m (obviously) not that computer savvy!

    Triple-click the line below to select it:
    /System/Library/CoreServices/Directory Utility.app
    Rght-click or control-click the highlighted text and select
    Services ▹ Open
    from the contextual menu.* The application Directory Utility will open.
    In the Directory Utility window, click the lock icon and authenticate. Select the Directory Editor tool in the toolbar. Select Users from the Viewing menu in the toolbar, if not already selected. Select the affected user account in the list. On the right is a list of properties and values. Select the property "HomeDirectory" and delete it by clicking the minus-sign icon directly below the property list. There are two such icons in the window. You want the one on the right, not the one on the left.
    CAUTION: Do not click the minus-sign icon on the left, below the user list.
    Then click the Save button in the lower right corner of the window. Quit Directory Utility.
    CAUTION: There is no "undo" in Directory Utility. If you make a mistake and delete something in the Directory Editor that should not have been deleted, restore your whole system from a backup and start over. I have no other help to offer in that case.
    Try again to log in as the affected user. If you can log in, you should find a file in the home folder with the name of the user and the filename extension "sparseimage" or "sparsebundle". Double-click that file. A disk image should open. Copy your files from that disk image to restore them.
    I strongly suggest that you deactivate legacy FileVault in the Security & Privacy preference pane, then log out and log back in. Consider activating FileVault 2 if you want that kind of security.
    *If you don't see the contextual menu item, copy the selected text to the Clipboard (command-C). Open a TextEdit window and paste into it (command-V). Select the line you just pasted and continue as above.

  • Plugins shown missing in Logic pro x when they are there ....Plz help

    I installed Melodyne when I just had Garageband....it worked
    when I installed Logic pro 9 ....Melodyne stopped loading, I installed vanguard at this stage
    When I installed Logic pro X .....error for both Melodyne and Vanguard was --> plugins not on your computer...now both melodyne and vanguard are missing plugins according to Logic pro x but it can't be as both these softwares work in Garageband or Logic pro 9...plz help .....

    Additional Info:
    As there is a common misunderstanding about VST plugins.....
    JBridgeM... which is the Mac version of JBridge.... is only for converting Mac 32 bit VST plugins to Mac 64bit VST plugins.. so they can be used in a 64bit Mac DAW that uses VST plugins.. An example would be Live 9 64bit for Mac
    It does not convert Windows/PC VST plugins... so you can use them on a Mac running OS X.....
    JBridge for Windows... only converts Windows 32bit VST plugins for use in Windows 64bit DAWs.... and so again this will not work on Macs running OS X
    ...and to answer your question about older projects that used Vanguard and 32 Lives.... Yes, it would work automatically... when you load one of those older projects

  • HT1937 Hi Dear, My Wifi and Bluetooth is not on what can do plz help me!

    Hi i have iPhone 4s since 4 month my Wi-Fi and bluetooth is not connceting or on what do i do plz help i am very upset

    Seems kind of obvious, turn them on.

  • I forgot my personal questions on my acount and dont know what to do plz help

    i forgot my personal questions in my acount and i dont know what to do plz help

    Forgotten Security Questions / Answers...
    See Here > Apple ID: Contacting Apple for help with Apple ID account security
              Ask to speak with the Account Security Team...
    Or Email Here  >  Apple  Support  iTunes Store  Contact
    More Info >  Apple ID: All about Apple ID security questions
    Note:
    You can only set up and/or change a Rescue Email Before you forget the questions/answers.

  • Cant make any phone calls...please help

    when i go to the phone i cannot pick a contact, edit a contact, dial a number, basically i cant use any phone features. nothing. i cant use favorites, recents, keypad, or voicemail, the only thing i can do is scroll my contacts, and go back home. everything else on the "phone" works fine, except for this pretty important feature. i can answer calls, but not make them. my "phone" is only been out the box barely 24 hours and this already happened...anybody help?

    I just had the same problem. The way I resolved it was by holding the button on the top down for several seconds. You are then prompted to "slide to power off". Power it off, and then press the button on the top again to restart your iphone. I was then able to use all of the phone functions again.

  • I forget my iphone 5 passward and i cant remove the ownership passward and so plz help me this topic coz what can i do?

    i forget my iphone 5 passward and i can't rem ove it because i update the software and when my phone finally done then asking the ownership passward so i forget it.  so what can i do i solved this probleum.  thanks
    i buy this phone in my friend so my friend is not here so i will be waiting your cute answers
    thank's

    Hi bilalusman,
    If you are having issues with the password on your iPhone, you may find the following article helpful:
    iOS: Forgotten passcode or device disabled after entering wrong passcode
    http://support.apple.com/kb/ht1212
    Regards,
    - Brenden

  • Cant make calls after updating my software today, help

    i updated my iphone 4 today after it automatically suggested it, now i can't make calls? help

    You're going to have to spend some time cleaning up some stuff.  See this article. 

  • I used to find all kinds of free software downloads on the apple site for my mac. games- everything...cant find it anymore. what the ...help??

    where did the free downloads go on apple site??? it had every catagory possible. pls help

    So am I. No need to be condescending to an OP.
    A number of free games for the Mac can be downloaded from here:
    http://www.downloadfreemacgames.com/

  • I lost my iphone and i couldnt track it because the one who stole it shut it down,, what to do plz help

    i lost my iphone and i couldnt track it because the one who stole it shut it

    you've lost you're phone, you can report it stolen but i doubt they will find the person, or if you had insurance use that. there is like a 1% chance you will find it. &like someone else said, the theif cant use it if it had a passcode lock or without your apple id and password

  • Error web.xml  customized ..... Plz help

    Hi
    I would like to handle custom message on exception raised by web container such as 404.
    Will this code om mine work (coded in web.xml )
    Please some body help
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
    <error-code>404</error-code>
    <location>/common/ErrorPage.jsp</location>
    <init-param>
         <param-name>NEWERROR</param-name>
         <param-value>NEWERRPGE</param-value>
    </init-param>
         <init-param>
         <param-name>VSERRNO</param-name>
         <param-value>pAGE UNDER CONSTRUCTION</param-value>
    </init-param>
    </error-page>Please some body help ASP
    with regards
    Karthik

    Hi
    I would like to handle custom message on exception raised by web container such as 404.
    Will this code om mine work (coded in web.xml )
    Although the Response was able to go to Error.jsp file,the params/values could not be retrieved.
    Please some body help
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
    <error-code>404</error-code>
    <location>/common/ErrorPage.jsp</location>
    <init-param>
    <param-name>NEWERROR</param-name>
    <param-value>NEWERRPGE</param-value>
    </init-param>
    <init-param>
    <param-name>VSERRNO</param-name>
    <param-value>pAGE UNDER CONSTRUCTION</param-value>
    </init-param>
    </error-page>
    Please some body help ASP
    with regards
    Karthik

  • DSN not found error and no default driver specified....plz help me

    am doing my project in jsp. using tomcat.
    am connecting the database using access.
    it cause the error like " DSN not found error"
    am verified so many times the data source name as well as data base connection.
    but not to be clear the error.
    am also tried it in weblogic, it worked perfectly.
    then am reinstall the tomcat and used other platform but not worth.
    plzzzzzzzzzz some one help me to clear the error.
    thanx in advance.
    with regards,
    senthil

    Yes, it has a DSN. It is:
    DSN Test Homer
    User Name ******
    Server homer.cs.odu.edu
    The connection string is:
    Provider=MSDASQL.1;Password=*******;Persist Security Info=True;User ID=******;Extended Properties="DSN=Test Homer;UID=******;PWD=******;SERVER=homer.cs.odu.edu;"
    with the appropriate user name and password filled in.

Maybe you are looking for

  • Problem while creating a z-table

    Hi Experts, when i created a z-table i am getting warnig message like "enhancement Category for table missing" what does it mean ? hoe to resolve that?..But the table was activated.. Regards Ravi.

  • JMS to Synchronous Web Service Scenario (Need Advice to use BPM or Not)

    Hi Experts, We have a scenario where we fetch data from JMS Sender and Send it to MDM Webservices. We want to have the files processed in such a way that until we get a response from webservice back that it was sucessful ,then only we need to process

  • Hardware problem with my iPhone 5 got swapped but not getting warranty

    I recently got swapped my iPhone 5 which was having harware issue and was out of warranty with a new device but not getting warranty on new device. Is this expected or there is some issue due to which I dont see even 90 day complementary warranty?

  • HDCam 1080i to ???? for PC and still look good compressed?

    Client has asked for 1280x720 compressed in a codec that a PC can read via After Effects. Eventually they want to go to Flash. Their source is HDCam 1080i. I've captured via FCP 6/AJA Kona LHe in Apple 10bit 4:2:2 Uncompressed, then let Compressor 3.

  • How to append data in same log after start and stop signal express

    how can i append my data to the same log file after starting and stopping the signal express ? Is there some option to do this. After start/stop it always makes a new log file. How can append datat to the same log file.  Thanks in advance.  Munir