Jankengame.java
import java.util.Scanner;
public class Jankengeme {
public static void main(String[] args) {
int playerHand;
int cpuHand;
Scanner sc = new Scanner(System.in);
String rpfHands[] = {"Goo","Choki","Par"};
int replay;
int replay2=1;
try {
do{
//Player-side processing
System.out.println("Please enter a number. 0: Goo, 1: Choki, 2: Par");
playerHand = sc.nextInt(); //Input numbers with standard input
System.out.println("The hand you put out" + rpfHands[playerHand]); //Hand output of the player
//Computer-side processing
cpuHand = (int)(Math.random()*3); //Random method to decide what to do with the computer
System.out.println("The hand of the computer" + rpfHands[cpuHand]); //Computer manual output
//Judgment
Result result1 = new Result();
result1.result(cpuHand,playerHand);
System.out.println("Would you like to try again? 0: Yes 1: No");
replay = sc.nextInt();
}while(replay!=replay2);
} catch( java.lang.ArrayIndexOutOfBoundsException e) {
System.err.println("error" + e.getMessage());
} finally {
System.out.println("Finish");
}
sc.close();
}
}
Result.java
public class Result {
String result;
int rock = 0;
int scissors = 1;
int paper = 2;
public void result(int a, int b){
if(a == b){
result = "I'm Aiko";
}else if((a == rock && b == scissors) || (a == scissors && b == paper ) ||
(a == paper && b == rock )){
result = "Computer wins. You lose";
}else if((a == rock && b == paper) || (a == scissors && b == rock ) ||
(a == paper && b == scissors )){
result = "You win. Computer losing";
}
System.out.println(result);
}
}
Recommended Posts