Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class RockPaperScissors {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Random random = new Random();
- int rounds;
- do {
- System.out.print("Add meg, hány kört szeretnél játszani: ");
- rounds = scanner.nextInt();
- } while (rounds <= 0);
- int userWins = 0;
- int computerWins = 0;
- int draws = 0;
- for (int i = 1; i <= rounds; i++) {
- System.out.println("Kör " + i + ":");
- System.out.print("Válasz (K=kő, P=papír, O=olló): ");
- String userChoice = scanner.next().toUpperCase();
- while (!userChoice.equals("K") && !userChoice.equals("P") && !userChoice.equals("O")) {
- System.out.print("Hibás válasz, add meg újra (K/P/O): ");
- userChoice = scanner.next().toUpperCase();
- }
- int computerChoiceInt = random.nextInt(3);
- String computerChoice = "";
- switch (computerChoiceInt) {
- case 0:
- computerChoice = "K";
- break;
- case 1:
- computerChoice = "P";
- break;
- case 2:
- computerChoice = "O";
- break;
- }
- System.out.println("A számítógép választása: " + computerChoice);
- if (userChoice.equals(computerChoice)) {
- System.out.println("Döntetlen!");
- draws++;
- } else if ((userChoice.equals("K") && computerChoice.equals("O"))
- || (userChoice.equals("P") && computerChoice.equals("K"))
- || (userChoice.equals("O") && computerChoice.equals("P"))) {
- System.out.println("Te nyertél!");
- userWins++;
- } else {
- System.out.println("A számítógép nyert!");
- computerWins++;
- }
- }
- System.out.println("\nEredmények:");
- System.out.println("Te nyertél: " + userWins);
- System.out.println("A számítógép nyert: " + computerWins);
- System.out.println("Döntetlen: " + draws);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement