import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main
(String[] args
) { Scanner scan
= new Scanner
(System.
in);
System.
out.
println("コンピュータとじゃんけんをします。"); System.
out.
println("対戦するコンピュータの人数を入力して下さい。(9人以内)"); String cpuNumStr
= scan.
nextLine(); int cpuNum = 0;
if (cpuNumStr.matches("[1-9]"))
cpuNum
= Integer.
parseInt(cpuNumStr
); else {
System.
out.
println("1から9までの数字を入力して下さい。終了します。"); }
int cpuHands[] = new int[cpuNum];
int gu = 0;
int tyoki = 0;
int pa = 0;
int numOfSuv = 1+cpuNum;
System.
out.
println("グー、チョキ、パーのいずれかをカタカナで入力して下さい。"); String yourHand
= scan.
nextLine(); int yourHandNum = 3;
if (yourHand.equals("グー")) {
yourHandNum = 0;
gu++;
}
else if (yourHand.equals("チョキ")) {
yourHandNum = 1;
tyoki++;
}
else if (yourHand.equals("パー")) {
yourHandNum = 2;
pa++;
}
else {
System.
out.
println("グー、チョキ、パーのいずれかをカタカナ入力して下さい。終了します。"); }
for (int i=0; i<cpuHands.length; i++)
cpuHands[i] = rand.nextInt(3);
System.
out.
println("あなたは"+yourHand
+"です。"); for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]==0) {
System.
out.
println("コンピュータ"+i
+"はグーです。"); gu++;
}
else if (cpuHands[i]==1) {
System.
out.
println("コンピュータ"+i
+"はチョキです。"); tyoki++;
}
else { // cpuHands[i]==2
System.
out.
println("コンピュータ"+i
+"はパーです。"); pa++;
}
}
if ((gu>=1 && tyoki>=1 && pa>=1) || tyoki+pa==0 || pa+gu==0 || gu+tyoki==0)
System.
out.
println("あいこです。"+"\n"); else if (pa==0) { // グーが勝ちの時
if (yourHandNum==0)
else {
yourHandNum=3; // 離脱
numOfSuv--;
}
for (int i=0; i<cpuHands.length;i++) {
if (cpuHands[i]==0)
System.
out.
print("コンピュータ"+i
+" "); else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
else if (gu==0) { // チョキが勝ちの時
if (yourHandNum==1)
else {
yourHandNum=3;
numOfSuv--;
}
for (int i=0; i<cpuHands.length;i++) {
if (cpuHands[i]==1)
System.
out.
print("コンピュータ"+i
+" "); else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
else { // パーが勝ちの時
if (yourHandNum==2)
else {
yourHandNum=3;
numOfSuv--;
}
for (int i=0; i<cpuHands.length;i++) {
if (cpuHands[i]==2)
System.
out.
print("コンピュータ"+i
+" "); else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
if (numOfSuv==1) {
System.
out.
println("最終勝者が確定したので終了します。"); }
while (numOfSuv!=1) {
gu = 0;
tyoki = 0;
pa = 0;
if (yourHandNum!=3) {
System.
out.
println("グー、チョキ、パーのいずれかをカタカナで入力して下さい。"); yourHand = scan.nextLine();
yourHandNum = 3;
if (yourHand.equals("グー")) {
yourHandNum = 0;
gu++;
}
else if (yourHand.equals("チョキ")) {
yourHandNum = 1;
tyoki++;
}
else if (yourHand.equals("パー")) {
yourHandNum = 2;
pa++;
}
else {
System.
out.
println("グー、チョキ、パーのいずれかをカタカナ入力して下さい。終了します。"); }
System.
out.
println("あなたは"+yourHand
+"です。"); }
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]!=3)
cpuHands[i] = rand.nextInt(3);
}
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]==0) {
System.
out.
println("コンピュータ"+i
+"はグーです。"); gu++;
}
else if (cpuHands[i]==1) {
System.
out.
println("コンピュータ"+i
+"はチョキです。"); tyoki++;
}
else if (cpuHands[i]==2) {
System.
out.
println("コンピュータ"+i
+"はパーです。"); pa++;
}
}
if ((gu>=1 && tyoki>=1 && pa>=1) || tyoki+pa==0 || pa+gu==0 || gu+tyoki==0)
System.
out.
println("あいこです。"+"\n"); else if (pa==0) { // グーが勝ちの時
if (yourHandNum==0)
else if (yourHandNum==3) {
;
} else { // 離脱
yourHandNum=3;
numOfSuv--;
}
for (int i=0; i<cpuHands.length;i++) {
if (cpuHands[i]==0)
System.
out.
print("コンピュータ"+i
+" "); else if (cpuHands[i]==3) {
;
} else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
else if (gu==0) { // チョキが勝ちの時
if (yourHandNum==1)
else if (yourHandNum==3) {
;
} else {
yourHandNum=3;
numOfSuv--;
}
for (int i=0; i<cpuHands.length;i++) {
if (cpuHands[i]==1)
System.
out.
print("コンピュータ"+i
+" "); else if (cpuHands[i]==3) {
;
} else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
else { // パーが勝ちの時
if (yourHandNum==2)
else if (yourHandNum==3) {
;
} else {
yourHandNum=3;
numOfSuv--;
}
for (int i=0; i<cpuHands.length;i++) {
if (cpuHands[i]==2)
System.
out.
print("コンピュータ"+i
+" "); else if (cpuHands[i]==3) {
;
} else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
if (numOfSuv==1) {
System.
out.
println("最終勝者が確定したので終了します。"); }
}
}
}