Featured
- Get link
- X
- Other Apps
Rock, Paper, Scissor Game in Java
We can make a simple rock, paper, scissor game in java. Heres, the code:
import java.util.Scanner;
import javax.lang.model.util.ElementScanner14;
import java.util.Random;
public class App {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int rand_1 = rand.nextInt(3);
System.out.println("\nRock, Paper, Scissior Game");
int trial = 1;
int player_score = 0;
int computer_score = 0;
while (trial<=5) {
System.out.println("\n0) Rock");
System.out.print("1) Paper");
System.out.print("\n2) Scissior");
System.out.print("\nEnter the choice number: ");
int choice = scan.nextInt();
// System.out.println(rand_1);
if (choice == rand_1) {
System.out.println("Player and Computer Selected Same ");
// System.out.println("Computer Choice: "+ rand_1);
} else {
switch (choice) {
case 0:
System.out.println("Computer choice: "+ rand_1);
if(rand_1<2){
if(0<rand_1){
System.out.println("Computer wins Match round "+ trial);
computer_score = computer_score+1;
}
}else{
System.out.println("Player wins Match round "+ trial);
player_score = player_score+1;
}
break;
case 1:
System.out.println("Computer choice: "+ rand_1);
if(0<rand_1){
if(1<rand_1){
System.out.println("Computer wins Match round "+ trial);
computer_score = computer_score+1;
}
}else{
System.out.println("Player wins Match round "+ trial);
player_score = player_score+1;
}
break;
case 2:
System.out.println("Computer choice: "+ rand_1);
if(rand_1<1){
if(rand_1<2){
System.out.println("Computer wins Match round "+ trial);
computer_score = computer_score+1;
}
}else{
System.out.println("Player wins Match round "+ trial);
player_score = player_score+1;
}
break;
default:
System.out.println("Wrong input");
break;
}
}
trial = trial+1;
}
System.out.println("\nPlayer Score: "+ player_score);
System.out.println("Computer Score: "+computer_score);
if (player_score>computer_score) {
int sp = player_score-computer_score;
System.out.println("\nPlayer won the game by " + sp);
} else if(computer_score>player_score) {
int cp = computer_score-player_score;
System.out.println("\nPlayer lose the game by "+cp);
}else{
System.out.println("\nNo one wins the game");
}
}
}
- Get link
- X
- Other Apps
Popular Posts
Changing text into voice using Javascript just in browser's console
- Get link
- X
- Other Apps
Comments
Post a Comment