Advertisement
AngyalRobert

RockPaperScissors by chatGpt

Feb 16th, 2023
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | Source Code | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class RockPaperScissors {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         Random random = new Random();
  8.        
  9.         int rounds;
  10.         do {
  11.             System.out.print("Add meg, hány kört szeretnél játszani: ");
  12.             rounds = scanner.nextInt();
  13.         } while (rounds <= 0);
  14.        
  15.         int userWins = 0;
  16.         int computerWins = 0;
  17.         int draws = 0;
  18.         for (int i = 1; i <= rounds; i++) {
  19.             System.out.println("Kör " + i + ":");
  20.             System.out.print("Válasz (K=kő, P=papír, O=olló): ");
  21.             String userChoice = scanner.next().toUpperCase();
  22.             while (!userChoice.equals("K") && !userChoice.equals("P") && !userChoice.equals("O")) {
  23.                 System.out.print("Hibás válasz, add meg újra (K/P/O): ");
  24.                 userChoice = scanner.next().toUpperCase();
  25.             }
  26.            
  27.             int computerChoiceInt = random.nextInt(3);
  28.             String computerChoice = "";
  29.             switch (computerChoiceInt) {
  30.                 case 0:
  31.                     computerChoice = "K";
  32.                     break;
  33.                 case 1:
  34.                     computerChoice = "P";
  35.                     break;
  36.                 case 2:
  37.                     computerChoice = "O";
  38.                     break;
  39.             }
  40.             System.out.println("A számítógép választása: " + computerChoice);
  41.            
  42.             if (userChoice.equals(computerChoice)) {
  43.                 System.out.println("Döntetlen!");
  44.                 draws++;
  45.             } else if ((userChoice.equals("K") && computerChoice.equals("O"))
  46.                     || (userChoice.equals("P") && computerChoice.equals("K"))
  47.                     || (userChoice.equals("O") && computerChoice.equals("P"))) {
  48.                 System.out.println("Te nyertél!");
  49.                 userWins++;
  50.             } else {
  51.                 System.out.println("A számítógép nyert!");
  52.                 computerWins++;
  53.             }
  54.         }
  55.        
  56.         System.out.println("\nEredmények:");
  57.         System.out.println("Te nyertél: " + userWins);
  58.         System.out.println("A számítógép nyert: " + computerWins);
  59.         System.out.println("Döntetlen: " + draws);
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement