Page 61 - CITS - CSA - TP (Volume 2) - Hindi
P. 61

कं  ूटर सॉ वेयर ए ीके शन - CITS



           टा  2: do-while loop का उपयोग करके  अनुमान लगाने का गेम

           import java.util.Random;
           importjava.util.Scanner;
           public class DoWhileGuessingGame {
           public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
             Random random = new Random();

           intsecretNumber = random.nextInt(100) + 1;
           int guess;
           int attempts = 0;
           System.out.println(“Guess the secret number between 1 and 100.”);
           do {

           System.out.print(“Enter your guess: “);
           guess = scanner.nextInt();
           attempts++;
           if (guess <secretNumber) {
           System.out.println(“Too low! Try again.”);
                    } else if (guess >secretNumber) {

           System.out.println(“Too high! Try again.”);
                    } else {
           System.out.println(“Congratulations! You guessed the secret number in “ + attempts + “ attempts.”);
                    }
                } while (guess != secretNumber);

           scanner.close();
              }
           }
           आउटपुट:































                                                           47

                                   CITS : IT & ITES - कं  ूटर सॉ वेयर ए ीके शन - अ ास 87
   56   57   58   59   60   61   62   63   64   65   66