Java Tutorial
Rock Paper Scissors Game in Java
import two packages
import java.util.Scanner;
import java.util.Random;
Source code:
import java.util.Scanner;
import java.util.Random;
public class rock_paper_scissor {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Start Game");
Random rn = new Random();
int randNum = rn.nextInt(1,4);
/*
* rock = 1
* paper = 2
* siccsor = 3
*/
int userCount = 0;
int computerCount = 0;
int i = 1;
while (i <= 5) {
int userVal = sc.nextInt();
System.out.println(randNum);
if (userVal == randNum) {
System.out.println("tie");
continue;
}
switch (userVal) {
case 1:
System.out.println("Rock");
if (randNum == 2) {
System.out.println("you lose");
computerCount++;
System.out
.println("User count is " + userCount + " || " +
"Computer count is " + computerCount);
} else {
System.out.println("you win");
userCount++;
System.out
.println("User count is " + userCount + " || " +
"Computer count is " + computerCount);
}
break;
case 2:
System.out.println("Paper");
if (randNum == 3) {
System.out.println("you lose");
computerCount++;
System.out
.println("User count is " + userCount + " || " +
"Computer count is " + computerCount);
} else {
System.out.println("you win");
userCount++;
System.out
.println("User count is " + userCount + " || " +
"Computer count is " + computerCount);
}
break;
case 3:
System.out.println("siccsor");
if (randNum == 1) {
System.out.println("you lose");
computerCount++;
System.out
.println("User count is " + userCount + " || " +
"Computer count is " + computerCount);
} else {
System.out.println("you win");
userCount++;
System.out
.println("User count is " + userCount + " || " +
"Computer count is " + computerCount);
}
break;
default:
System.out.println("Invalid number");
}
i++;
}
System.out.println("----------Final---------");
if (userCount == computerCount) {
System.out.println("Tie");
} else if (userCount > computerCount) {
System.out.println("You won the match");
} else {
System.out.println("Computer won the match");
}
}
}
Description:
This project is a simple Rock Paper Scissors game written in Java. The game allows two players to compete against each other, and the winner is the player who gets the most points. The game is played by each player choosing one of the three options: rock, paper, or scissors. The game then randomly chooses one of the three options for the computer player. The winner is determined by the following rules
- Rock beats scissors.
- Scissors beats paper.
- Paper beats rock.
The game keeps track of the score for each player, and the player with the most points at the end of the game wins.
This project is a simple Rock Paper Scissors game written in Java. The game allows two players to compete against each other, and the winner is the player who gets the most points. The game is played by each player choosing one of the three options: rock, paper, or scissors. The game then randomly chooses one of the three options for the computer player. The winner is determined by the following rules
- Rock beats scissors.
- Scissors beats paper.
- Paper beats rock.
The game keeps track of the score for each player, and the player with the most points at the end of the game wins.
0 Comments