import java.util.Scanner;
import java.util.Random;
public class Test{
public static void main(String args[]){
Scanner msc = new Scanner(System.in);
//化繁为简,先死后活
// Random rand = new Random();
// System.out.println(rand.nextInt(3)+1);
Random rand = new Random();
System.out.println("你出什么?");
int peo = msc.nextInt();
game g = new game(peo,rand.nextInt(3));
char c = ' ';
do{
g.guss();
System.out.println("你还玩吗y/n");
c = msc.next().charAt(0);
}while(c == 'y');
}
}
class game{
int peo;
int com;
public game(int peo,int com){
this.peo = peo;
this.com = com;
}
public void guss(){
if(peo == com){
System.out.println("平局");
}else if((peo == 0 && com == 1) ||
(peo == 1 && com == 2) || (peo == 2 && com == 0)){
System.out.println("人影了");
}else{
System.out.println("人输了");
}
switch(peo){
case 0:
System.out.println("人出的石头");
break;
case 1:
System.out.println("人出的剪刀");
break;
case 2:
System.out.println("人出的布");
break;
}
switch(com){
case 0:
System.out.println("电脑出的石头");
break;
case 1:
System.out.println("电脑出的剪刀");
break;
case 2:
System.out.println("电脑出的布");
break;
}
}
}
还可以优化一下,比如如果输出的不是n,是不是可以做一个异常处理,但是我还没学到异常
5163

被折叠的 条评论
为什么被折叠?



