<メモ>
じゃんけんゲーム
言語:Java
環境:Eclipse
作業時間:約3時間半
Person.java
class Person {
public void you() {
Scanner scan = new Scanner(System.in);
ここ→ 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("なにを出しますか 0:グー、1:チョキ、2:パー ");
System.out.print("数字を入力してください:");
player.you();
computer.com();
int you =player.getYou();
int com =computer.getCom();
result(you,com);
Scanner re = new Scanner(System.in);
System.out.println("もう一度遊ぶ? はい:0/いいえ:1");
retry = re.nextInt();
}while(retry!=noRetry);
System.out.println("終了");
}
public static void result(int you, int com) {
if(you == com) {
System.out.println("あいこです");
}else if((you == 0 && com == 1)||(you == 1 && com == 2)||(you == 2 && com == 0) ) {
System.out.println("あなたの勝ちです");
}else if((you == 0 && com == 2)||(you == 1 && com == 0)||(you == 2 && com == 1)){
System.out.println("あなたの負けです");
}
}
}
Person.java
import java.util.Random;
import java.util.Scanner;
class Person {
private int you;
private int ran;
String[]hands = {"グー","チョキ","パー"};
public void you() {
Scanner scan = new Scanner(System.in);
you = scan.nextInt();
if(you == 0) {
System.out.println("あなたは"+hands[you]+"をだしました");
}else if(you == 1) {
System.out.println("あなたは"+hands[you]+"をだしました");
}else if(you == 2){
System.out.println("あなたは"+hands[you]+"をだしました");
}else{
System.out.println("入力された数値は無効です");
}
}
public void com() {
Random random = new Random();
ran = random.nextInt(3); //0以上3未満の数値をランダムに選ぶ
if(ran == 0) {
System.out.println("相手は"+hands[ran]+"をだしました");
}else if(ran == 1) {
System.out.println("相手は"+hands[ran]+"をだしました");
}else if(ran == 2){
System.out.println("相手は"+hands[ran]+"をだしました");
}
}
public int getYou() {
return you;
}
public int getCom() {
return ran;
}
}