जावा में पासा फेंकने का अनुकरण

Oct 30 2020

मेरी प्रयोगशालाओं में से एक के लिए मुझे एक पूर्ण जावा प्रोग्राम लिखना होगा जो "बहुत ही सरल" पासा सट्टेबाजी के खेल का अनुकरण करेगा। विनिर्देशों इस प्रकार हैं: 3 पासा के फेंक अनुकरण। अगर आप तीन छक्के लगाते हैं तो आप जीत जाते हैं$20; if you throw three of any other value then you win $10; यदि आप दो पासे फेंकते हैं जो समान मूल्य हैं तो आप जीत जाते हैं$5. If none of the conditions above are met, then you would lose $१।

उदाहरण रन: पासा फेंको

Dice 1 : 2     Dice 2 : 1    Dice 3 : 2
Congratulations : You threw TWO - 2s
You win $5 Dice 1 : 2 Dice 2 : 2 Dice 3 : 2 Congratulations : You threw THREE - 2s You win $10

Dice 1 : 4    Dice 2 : 6    Dice 3 : 3
Unfortunately : You did not throw anything of value
You lose $1

इस समस्या को हल करने के लिए मैंने जो कोड लिखा था वह इस प्रकार है:

/**
 * SRN: 507-147-9
 */
public class Lab6_part3 {

    public static void main(String[] args) {

        // define vars
        int round = 1;
        int dice1, dice2, dice3;

        while (round <= 3) {

            dice1 = 1 + (int)(Math.random() * 6);
            dice2 = 1 + (int)(Math.random() * 6);
            dice3 = 1 + (int)(Math.random() * 6);

            System.out.println();
            System.out.println("Dice 1 : " + dice1 + "    Dice 2 : " + dice2 + "    Dice 3 : " + dice3);

            // Three of a kind
            if ((dice1 == dice2) && (dice1 == dice3)) { // 3-of-a-kind (D1)                              

                // Rolls three sixes
                if (dice1 == 6) {
                    System.out.println("Congratulations : You threw THREE - 6s");
                    System.out.println("You win $20");
                } else {
                    // Rolls three of anything else
                    System.out.println("Congratulations : You threw THREE - " + dice1 + "s");
                    System.out.println("You win $10"); } } // Two of a kind (PRINT "dice1") else if (dice1 == dice2 || dice1 == dice3) { System.out.println("Congratulations : You threw TWO - " + dice1 + "s"); System.out.println("You win $5");
            } 
            
            // Two of a kind (PRINT "dice2")
            else if (dice2 == dice1 || dice2 == dice3) {                              
                System.out.println("Congratulations : You threw TWO - " + dice2 + "s");
                System.out.println("You win $5"); } // Two of a kind (PRINT "dice3") else if (dice3 == dice1 || dice3 == dice2) { System.out.println("Congratulations : You threw TWO - " + dice3 + "s"); System.out.println("You win $5");
            } 
            // Did not throw anything of value
            else {
                System.out.println("Unfortunately : You did not throw anything of value");
                System.out.println("You lose $1");
            }
            round++;
        }
    }
}

इस समस्या को मैं इस दृष्टिकोण के साथ चला रहा हूं, हालांकि यह है कि कोड कार्य, जैसा कि माना जाता है, मैं तीन "के बजाय" यदि "कथनों को लिखने के लिए एक सरल तरीका है। इसमें मेरा लक्ष्य है, पासा 2 और 3 के साथ पासा 1 की तुलना करने और पासा 1 और 3 के साथ पासा 2, आदि के बजाय एक तीन-तरफा "या" कथन बनाना है ...

else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3) {                              
    System.out.println("Congratulations : You threw TWO - " + somethings?? + "s");
    System.out.println("You win $5"); 

अगर मैं ऐसा करता, तो मैं उस जोड़ी के मूल्य को कैसे प्रिंट कर पाता जो मैंने पहचाना है?

जवाब

7 Marc Oct 30 2020 at 14:37

CodeReview में आपका स्वागत है। अपने मुद्दे के बारे में, एक त्वरित समाधान है:

else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3) {       
    int doubleNum = dice1 == dice2 ? dice1 : dice1 == dice3 ? dice1 : dice3;
    System.out.println("Congratulations : You threw TWO - " + doubleNum + "s");
    System.out.println("You win $5"); 
}

टर्नरी ऑपरेटर कोड को अधिक कॉम्पैक्ट बनाता है, लेकिन इस मामले में मुझे लगता है कि यह श्रृंखला की तुलना में थोड़ा कम पठनीय है if-else।

कुछ अन्य सुझाव:

  • जबकि लूप को एक अधिक सुविधाजनक फॉर-लूप द्वारा प्रतिस्थापित किया जा सकता है। से:
    int round = 1;
    while(round<=3){
        //...
        round++;
    }
    
    सेवा:
    for(int round = 1; round <= 3; round++) {
        //...
    }
    
  • एक पंक्ति में कई चर घोषित करने को जावा में अच्छा अभ्यासint dice1, dice2, dice3; नहीं माना जाता है ।
  • एक यादृच्छिक उत्पन्न floatकरने और फिर इसे कास्टिंग करने के बजाय int, सीधे intसाथ उत्पन्न करें ThreadLocalRandom.current().nextInt(6)। डॉक्स ।
  • आप पहली पंक्ति को प्रिंट कर सकते हैं System.out.format। से:
    System.out.println();
    System.out.println("Dice 1 : " + dice1 + "    Dice 2 : " + dice2 + "    Dice 3 : " + dice3);
    
    सेवा:
    System.out.format("%nDice 1 :  %d    Dice 2 : %d    Dice 3 : %n", dice1, dice2, dice3)
    
  • क्लास का नाम पास्कलकेस होना चाहिए । इसकी जगह Lab6_part3आप नाम रख सकते हैं Lab6Part3।
  • इसे बदलने के लिए आसान बनाने के लिए, राउंड की संख्या के लिए एक स्थिर घोषित करें। उदाहरण के लिए:
    public class Lab6Part3 {
    
        private static final int ROUNDS = 3;
    
        public static void main(String[] args) {
            for(int round = 1; round <= ROUNDS; round++) {
                //...
            }
    }
    

एक अन्य तरीका यह है Random#intsकि आवृत्तियों की गणना के साथ तीन संख्याएँ उत्पन्न करें :

Random r = new Random();
// Generate three random numbers from 1 to 6
IntStream diceRolls = r.ints(3, 1, 7);

// Generate map of frequencies
Map<Integer, Long> freq = diceRolls.boxed()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

boolean winner = false;
for (Entry<Integer, Long> entry : freq.entrySet()) {
    int number = entry.getKey();
    long times = entry.getValue();
    if (times == 3) {
        // Three of a kind ...
        winner = true;
    } else if (times == 2) {
        // Two of a kind...
        System.out.println("Congratulations : You threw TWO - " + number + "s");
        System.out.println("You win $5");
        winner = true;
    }
}
if(!winner) {
    // Did not throw anything of value
}
2 SavvasParastatidis Oct 30 2020 at 21:40

आपको 3 और के_ कथन होने की आवश्यकता नहीं है। 3 परिदृश्य हैं जहां आप "दो-एक-ए-तरह" मामले में हैं। dice1 == dice2, dice1 == dice3, dice2 == dice3। आपका पहला यदि ब्लॉक इन परिदृश्यों में से 2 को पकड़ता है। एकमात्र अन्य विकल्प यदि dice2 == dice3 है।

यदि आप इसके बारे में सोचते हैं, तो आपका तीसरा और_ऑफ ब्लॉक हमेशा पहले दो ब्लॉकों में से एक में सही मूल्यांकन करेगा, इसलिए आपका कोड कभी भी उस तक नहीं पहुंचेगा।

// Two of a kind (PRINT "dice1")
else if (dice1 == dice2 || dice1 == dice3) {
    System.out.println("Congratulations : You threw TWO - " + dice1 + "s");
    System.out.println("You win $5"); } // Two of a kind (PRINT "dice2") else if (dice2 == dice3) { System.out.println("Congratulations : You threw TWO - " + dice2 + "s"); System.out.println("You win $5");
}
2 mdfst13 Oct 31 2020 at 18:20
        }
        
        // Two of a kind (PRINT "dice1")
        else if (dice1 == dice2 || dice1 == dice3) {

कृपया ऐसा न करें। यदि आप अर्ध-पुच्छ का उपयोग करना चाहते हैं else, तो कृपया इसे हमेशा दो लगातार लाइनों पर लिखें।

        }
        else if (dice1 == dice2 || dice1 == dice3) {
          // Two of a kind (PRINT "dice1")

इसके दो कारण हैं।

  1. अगर मैं यह जानना चाहता हूं कि संरचना समाप्त हो गई है, तो इस तरह से मैं बस एक पंक्ति को अतीत से देख सकता हूं }। आपके मूल के साथ, मुझे अगले कथन के लिए मनमानी संख्या को देखना होगा। यह विशेष रूप से बुरा हो जाता है क्योंकि टिप्पणी ब्लॉक लंबे समय तक मिलता है। पूरी तरह से एक टिप्पणी लिखना संभव है जो एकल स्क्रीन की तुलना में लंबा है। इसका अर्थ है कि बस थोड़ा सा आगे बढ़ने के बजाय, मुझे यह देखने के लिए टिप्पणी को अतीत में डालना होगा कि कोई elseब्लॉक है।
  2. यदि कोई ऐसी चीज़ का उपयोग करता है जो सुधार को कोड को elseउसी लाइन पर रखने के लिए उपयोग करता है }, तो उसे टिप्पणी को वैसे भी स्थानांतरित करना होगा। तब भी अगर वे इसे वापस स्वरूपित करते हैं, तो टिप्पणी आगे बढ़ेगी। यह स्रोत नियंत्रण में भ्रम का कारण बनता है। तो बस यह तरीका है कि सुधारक शुरू से ही होगा।

सी-स्टाइल भाषाओं में सामान्य रूप से भ्रामक ब्लॉक हैंडलिंग होती है। क्योंकि वे }संरचना को समाप्त करने और निरंतर संरचना में ब्लॉक को समाप्त करने के लिए दोनों का उपयोग करते हैं । यानी उनके पास यह कहने का कोई तरीका नहीं है कि ifसंरचना किसी न elseकिसी प्रकार से जारी है । क्षतिपूर्ति करने के लिए, हमें कोडिंग सम्मेलनों का उपयोग करना होगा। और सबसे सरल सम्मेलनों में से एक, जो शैली को हस्तांतरित करता है, वह है कि कभी भी संबंधित कीवर्ड से एक पंक्ति से अधिक दूर एक घुंघराले ब्रेस ( }या {) न रखें, यदि एक है।