Memo
Rock-paper-scissors game
Language: Java
Environment: Eclipse
Working time: about 3.5 hours
Person.java
class Person {
public void you() {
Scanner scan = new Scanner(System.in);
Here → int you= scan.nextInt();
if(you == 0) { ......
Janken.java
import java.util.Scanner;
public class Janken {
public static void main(String[] args) {
Person player = new Person();
Person computer = new Person();
int retry;
int noRetry = 1;
do {System.out.println("What do you want to put out 0: Goo, 1: Choki, 2: Par");
System.out.print("Please enter a number:");
player.you();
computer.com();
int you =player.getYou();
int com =computer.getCom();
result(you,com);
Scanner re = new Scanner(System.in);
System.out.println("Play again? Yes: 0/No: 1");
retry = re.nextInt();
}while(retry!=noRetry);
System.out.println("End");
}
public static void result(int you, int com) {
if(you == com) {
System.out.println("I'm Aiko");
}else if((you == 0 && com == 1)||(you == 1 && com == 2)||(you == 2 && com == 0) ) {
System.out.println("You win");
}else if((you == 0 && com == 2)||(you == 1 && com == 0)||(you == 2 && com == 1)){
System.out.println("You lose");
}
}
}
Person.java
import java.util.Random;
import java.util.Scanner;
class Person {
private int you;
private int ran;
String[]hands = {"Goo","Choki","Par"};
public void you() {
Scanner scan = new Scanner(System.in);
you = scan.nextInt();
if(you == 0) {
System.out.println("you are"+hands[you]+"Was issued");
}else if(you == 1) {
System.out.println("you are"+hands[you]+"Was issued");
}else if(you == 2){
System.out.println("you are"+hands[you]+"Was issued");
}else{
System.out.println("The number entered is invalid");
}
}
public void com() {
Random random = new Random();
ran = random.nextInt(3); //Randomly select a number between 0 and 3
if(ran == 0) {
System.out.println("Opponent"+hands[ran]+"Was issued");
}else if(ran == 1) {
System.out.println("Opponent"+hands[ran]+"Was issued");
}else if(ran == 2){
System.out.println("Opponent"+hands[ran]+"Was issued");
}
}
public int getYou() {
return you;
}
public int getCom() {
return ran;
}
}
Recommended Posts