eddyboi Ierakstīts Janvāris 2, 2013 Share Ierakstīts Janvāris 2, 2013 Sveiki bootisti! Mācos java no grāmatas Addison Wesley Absolute Java 5th Edition 2012. Katras nodaļas beigās ir uzdevumi, taču atbildes pieejamas internetā, par cik grāmata ir nokačāta, nav pieejas šiem resursiem. Vēlos lūgt padomus kā uzlabot algoritmu šim uzdevumam un jautāt vai vispār mans risinājums ir pareizs - In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses. Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wager, the program should calculate whether the player would win or lose. The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games. Add c ounters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, compute the probability of winning [i.e.,Wins / (Wins + Losses)] and output this value. Over the long run, who is going to win the most games, you or the house? Mans risinājums import java.util.Random; public class Project2 { public static void main(String[] args) { int dice1,dice2,diceSum,count,win=0,loss=0,pointCounter=0,point; double probability; Random Randomgenerator=new Random(); for (count=0;count<10000;count++) { dice1=Randomgenerator.nextInt(6)+1; dice2=Randomgenerator.nextInt(6)+1; diceSum=dice1+dice2; //System.out.println("dice:"+diceSum); if (diceSum==7 || diceSum==11) win++; else if (diceSum==2 || diceSum==3 || diceSum==12) loss++; else { point=diceSum; pointCounter++; while (diceSum!=7||diceSum!=point) { dice1=Randomgenerator.nextInt(6)+1; dice2=Randomgenerator.nextInt(6)+1; diceSum=dice1+dice2; //System.out.println("dice point:"+diceSum); if (point==diceSum) { win++; break; } else if (diceSum==7) { loss++; break; } else; } } } probability=(win/(double)(count))*100; System.out.println("Wins:"+win); System.out.println("Losses:"+loss); System.out.println("Point:"+pointCounter); System.out.println("Probability:"+probability+"%"); } } Izvade- Wins:4917 Losses:5083 Point:6657 Probability:49.17% Link to comment Share on other sites More sharing options...
usver Janvāris 2, 2013 Share Janvāris 2, 2013 korektumā neiedziļinājos, bet daudz smukāk būtu pārbaudi iznest atsevišķā funkcijā. if ( checkTurn( dice1, dice2 ) ){ win++; } else { loss++; } boolean checkTurn(int myDice, int yourDice){ boolean iWon = true; // būsim optimisti .. .. return iWon; } Link to comment Share on other sites More sharing options...
eddyboi Janvāris 2, 2013 Author Share Janvāris 2, 2013 (labots) Boolean'u es nevaru izmatot, jo ārējā ir 3 pārbaudes - ja uzkrīt 7 vai 11; ja uzkrīt 2,3 vai 12; un visi parējie. Savukārt iekšējā arī ir 3 pārbaudes - ja uzkrīt 7, ja uzkrīt "point" vai uzkrīt jebkurš cits. Vai tad ne tā? Edit: Kāpēc tauta tik neatsaucīga? Labots Janvāris 2, 2013 - eddyboi Link to comment Share on other sites More sharing options...
valka Janvāris 4, 2013 Share Janvāris 4, 2013 usver ieteikumam nepievienojos (naf slēpt tik elementāru loģiku?), bet īstenot to varētu ar pārslogotām (overloaded) metodēm. Tas ir, metodēm ar vienādu nosaukumu, bet atšķirīgu parametru listi. Piemēram, boolean resolveRoll(int a, int b) { return (a == 7 || b == 11); } boolean resolveRoll(int a, int b, int c) { return (a == 2 || b == 3 || c == 12); } Kodā kļūdas nesaskatīju, un rezultāts arī atbilst gaidītajam. Vienīgais, ko pamanītu ir, while (diceSum!=7||diceSum!=point) { /* šo... */ } while (true) { /* uz šo */ } jo šī pārbaude ir bezjēdzīga, ja visa loģika jau ir iekš while bloka. Tas ļenganais else, while bloka beigās, arī naf nav vajadzīgs. Link to comment Share on other sites More sharing options...
eddyboi Janvāris 4, 2013 Author Share Janvāris 4, 2013 Jā par parametriem sāku lasīt nākošajā nodaļā. Taču nesaprotu kā darbosies cikls ar while(true),kad viņš būs false? Link to comment Share on other sites More sharing options...
valka Janvāris 5, 2013 Share Janvāris 5, 2013 (labots) Nekad. while bloks tiks izpildīts, kamēr nebūs patiess viens no diviem nosacījumiem, kas noved pie break izpildes, kas pārtrauc while, for un do-while ciklu, kura iekšienē tas atrodas. Tātad, kauliņi tiks mesti, kamēr to summa nebūs vienāda ar 7, un tiks ieskaitīts zaudējums, vai to summa būs vienāda ar mērķa punktu sakaitu, un tiks ieskaitīta uzvara. Šādi bezgalīgi while cikli, no kuriem izlaužas ar break palīdzību, kurš izpildās pie nosacījumiem, kas ir pārāk sarežģīti, lai tos sakaidri aprakstītu iekš while galvenes, ir klasiska un bieži sastopama programēšanas idioma. Labots Janvāris 5, 2013 - valka Link to comment Share on other sites More sharing options...
eddyboi Janvāris 5, 2013 Author Share Janvāris 5, 2013 Paldies par atbildi valka. Noteikti ņemšu vērā. Link to comment Share on other sites More sharing options...
Recommended Posts
Izveido kontu, vai pieraksties esošajā, lai komentētu
Jums ir jābūt šī foruma biedram, lai varētu komentēt tēmas
Izveidot jaunu kontu
Piereģistrējies un izveido jaunu kontu, tas būs viegli!
Reģistrēt jaunu kontuPierakstīties
Jums jau ir konts? Pierakstieties tajā šeit!
Pierakstīties tagad!