JAVA 猜拳游戏

package Problem_4;//包名请自动忽略


import java.util.Random;
import java.util.Scanner;


/*4.完成猜拳游戏
-----------------------------
请输入你的选择:
1. 石头
2. 剪刀
3. 布
-----------------------------
你的选择是【布】, 电脑的选择是【石头】
恭喜你获得了胜利!*/


public class Test {


public static void main(String[] args) {
// TODO Auto-generated method stub
view();
choose();
//playAagain();
}
public static void view(){
System.out.println("\r\n-------------------------------\r\n");
System.out.println("请输入数字代表你的选择:");
System.out.println("1.石头");
System.out.println("2.剪刀");
System.out.println("3.布");
System.out.println("\r\n-------------------------------\r\n");
}
//得到玩家出的拳
public static String player(int i){
String str1=null;
switch(i){
case 1:
str1="石头";
break;
case 2:
str1="剪刀";
break;
case 3:
str1="布";
break;
}
return str1;
}

//得到电脑随机出的拳
public static String computer(int c){
String str2 = null;//电脑选择的
switch(c){
case 1:
str2="石头";
break;
case 2:
str2="剪刀";
break;
case 3:
str2="布";
break;
}
return str2;
}


public static void choose(){
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
String str1 = player(i);//玩家选择的



Random rd=new Random();
int c=rd.nextInt(3)+1;
String str2=computer(c);//电脑选择的


//进行判断

if(!str1.equals(str2)){
if(i==1&&c==2){
System.out.println("你的选择是:"+str1+"\t"+"电脑选择的是:"+str2+"\r\n"+"恭喜你赢了");
}else if(i==1&&c==3){
System.out.println("你的选择是:"+str1+"\t"+"电脑选择的是:"+str2+"\r\n"+"你输了,请继续努力");
}else if(i==2&&c==1){
System.out.println("你的选择是:"+str1+"\t"+"电脑选择的是:"+str2+"\r\n"+"你输了,请继续努力");
}else if(i==2&&c==3){
System.out.println("你的选择是:"+str1+"\t"+"电脑选择的是:"+str2+"\r\n"+"恭喜你赢了");
}else if(i==3&&c==1){
System.out.println("你的选择是:"+str1+"\t"+"电脑选择的是:"+str2+"\r\n"+"恭喜你赢了");
}else if(i==3&&c==2){
System.out.println("你的选择是:"+str1+"\t"+"电脑选择的是:"+str2+"\r\n"+"你输了,请继续努力");
}
playAagain();
}else{
rechoose();//如果玩家和电脑出的一样的,就再次选择 也可以写成choose();

}
}


public static void rechoose(){
System.out.println("平局,请重新选择:");
view();
choose();
playAagain();
}

//重玩方法
public static void playAagain(){
System.out.println("\r\n------------------------------------\r\n");
System.out.println("是否要再玩一局:\r\n"
+ " 是:请输入Y,退出:请输入N");
Scanner sc=new Scanner(System.in);
String str3=sc.next();
if(str3.equals("Y")){
view();
choose();
}else if(str3.equals("N")){
System.gc();
}else{
System.out.println("输入有误请重新输入:");
playAagain();
}
}
}

猜你喜欢

转载自blog.csdn.net/hokinhu/article/details/76340367