1. 程式人生 > >java----猜拳(10局分勝負)

java----猜拳(10局分勝負)

import ID compute 石頭 ont i++ system.in and rand

package learning;
import java.util.*;
public class Guess{
public static void main(String [] args){
int j=0;
while(j<4){
int count1=0;
int count2=0;
int count3=0;
for(int i=1;i<11;i++){
System.out.println("-------猜拳遊戲-------");
System.out.println("第"+i+"局");
System.out.println("請出拳:(1.剪刀 2.石頭 3.布)");
Scanner in = new Scanner(System.in);
int person = in.nextInt();
int computer = (int)(Math.random()*3+1);
String marks = "拳頭";
String marks2 = "拳頭";
switch(person){
case 1:
marks = "剪刀";
break;
case 2:
marks = "石頭";
break;
case 3:
marks = "布";
break;
}
switch(computer){
case 1:
marks2 = "剪刀";
break;
case 2:
marks2 = "石頭";
break;
case 3:
marks2 = "布";
break;
}
if (person==computer){
System.out.println("平局,"+"你出的是"+marks+",電腦出的是"+marks2);
count1 =count1+1;
}
else if (person==1&&computer==2||person==2&&computer==3||person==3&&computer==1){
System.out.println("你輸了,"+"你出的是"+marks+",電腦出的是"+marks2);
count2 =count2+1;
}
else {
System.out.println("你贏了,"+"你出的是"+marks+",電腦出的是"+marks2);
count3 =count3+1;
}
System.out.println("你贏了"+count3+"局,"+"你輸了"+count2+"局,"+"平局"+count1+"局");
}
if(count3>count2){
System.out.println("恭喜你,你贏了!");
break;
}
if(count3<count2){
System.out.println("很遺憾,你輸了!");
break;
}
if(count3==count2){
System.out.println("棋逢對手,平局!再來一次吧!");
j++;
continue;
}
}
}
}

java----猜拳(10局分勝負)