人机猜拳游戏Java

作业要求:

我的代码:

package day20181119;
/**
* 猜拳游戏
* @author Administrator
* @version1.0
*/
import java.util.Scanner;
public class FingerGuessing {
String chName;
String name;
int i;
int ch1;
int Score1;
int ch2;
int Score2;
public void showLoginMenu(){
System.out.println("--------------欢迎来到游戏世界------------");
System.out.println("\t***********************");
System.out.println("\t *****猜拳,开始*****");
System.out.println("\t***********************");
System.out.println("\n出拳规则:1.剪刀2.石头3.布");
System.out.print("请选择对方角色:(1.刘备2.孙权3.曹操)");
Scanner input=new Scanner (System.in);
int ch=input.nextInt();
if(ch==1){
chName="刘备";
showStartGame();
}else if(ch==2){
chName="孙权";
showStartGame();
}else if(ch==3){
chName="曹操";
showStartGame();
}else{
System.out.println("您的输入有误,请重新输入!");
showLoginMenu();
}
}
public void showStartGame(){
Scanner input=new Scanner(System.in);
System.out.print("请输入您的姓名:");
name=input.nextLine();
System.out.println(name+"VS"+chName+"对战!");
showStart();
}
public void showStart(){
Scanner input=new Scanner(System.in);
i=i+1;
System.out.print("\n要开始第"+i+"轮吗?(Y/N)");
String ch=input.nextLine();
if(ch.equals("Y")){
showFist();
}else{
showResult2();
}
}
public void showFist(){
System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");
Scanner input=new Scanner(System.in);
ch1=input.nextInt();
if(ch1==1){
System.out.println("你出拳:剪刀");
showComputer();
}else if(ch1==2){
System.out.println("你出拳:石头");
showComputer();
}else if(ch1==3){
System.out.println("你出拳:布");
showComputer();
}
}
public void showComputer(){
ch2=(int)(Math.random()*3);
if(ch2==0){
System.out.println(chName+"出拳:剪刀");
showResult();
}else if(ch2==1){
System.out.println(chName+"出拳:石头");
showResult();
}else if(ch2==2){
System.out.println(chName+"出拳:布");
showResult();
}
}
public void showResult(){
if(ch1==1&&ch2==1||ch1==2&&ch2==2||ch1==3&&ch2==0){
System.out.println("结果是:^_^,你输了,下次加油啊!");
Score2++;
showStart();
}else if(ch1==1&&ch2==2||ch1==2&&ch2==0||ch1==3&&ch2==1){
System.out.println("结果是:恭喜,你赢了!");
Score1++;
showStart();
}else if(ch1==1&&ch2==0||ch1==2&&ch2==1||ch1==3&&ch2==2){
System.out.println("结果是:和局,真帅!");
showStart();
}
}
public void showResult2(){
System.out.println("------------------------------------");
System.out.println(name+"VS"+chName);
System.out.println("对战次数:"+(i-1));
System.out.println("姓名\t得分");
System.out.println(name+"\t"+Score1);
System.out.println(chName+"\t"+Score2);
if(Score1>Score2){
System.out.print("结果:恭喜恭喜!");
}else if(Score1<Score2){
System.out.println("结果:下次再加油哦!");
}else if(Score1==Score2){
System.out.println("真是皆大欢喜的结局呢!");
}
System.out.println("------------------------------------");
}
}

-------------------------------------------------------------------------------------

package day20181119;
public class TestFingerGuessing {
public static void main(String[] args) {
FingerGuessing game=new FingerGuessing();
game.showLoginMenu();
}

}

猜你喜欢

转载自www.cnblogs.com/SUN99bk/p/9988240.html