//第一部分
package com.company;
import java.util.Scanner;
public class Game {
int count=0;//对战次数
int num1;
int num2;
int score1=0;//成绩
int sxore2=0;
String computername=null;
String peoplename=null;
public void startGame() {
Scanner scanner = new Scanner(System.in);
System.out.println("你叫什么?");
peoplename = scanner.next();
System.out.println(peoplename+ ",欢迎你!");
System.out.println("****************");
System.out.println("猜拳,开始");
System.out.println("****************");
System.out.println("出拳规则:1.剪刀" + "\t" + "2.石头" + "\t" + "3.布");
System.out.println("请选择对方角色(1:刘备2:孙权3:曹操):");
int num=scanner.nextInt();
switch (num) {
case 1:
computername="刘备";
System.out.println("你"+"\t"+"vs"+"\t"+computername);
break;
case 2:
computername="孙权";
System.out.println("你" + "\t" + "vs" + "\t" + computername);
break;
case 3:
computername="曹操";
System.out.println("你" + "\t" + "vs" + "\t" + computername);
break;
}
System.out.println("要开始吗?(y/n)");
String shifou = scanner.next();
String shifou1;
if (shifou.equals("y")) {
do{
people();
Computer();
//判断
if(num1==1&&num2==3||num1==2&&num2==1||num1==3&&num2==2){
System.out.println("恭喜你赢了!");
score1++;
}else if(num1==1&&num2==1||num1==2&&num2==2||num1==3&&num2==3){
System.out.println("呀你们平局!");
score1++;
sxore2++;
}else if(num2==1&&num1==3||num2==2&&num1==1||num2==3&&num1==2){
System.out.println("对方获胜!");
sxore2++;
}
count++;//对战次数累加
System.out.println("是否进入下一轮?(y/n)");
shifou1=scanner.next();
}while ("y".equals(shifou1));
showResult();
}else {
System.out.println("退出系统,谢谢你的参与");
}
}
public void people(){
Scanner scanner=new Scanner(System.in);
System.out.println("请出拳1.剪刀" + "\t" + "2.石头" + "\t" + "3.布(输出相应数字):");
num1 = scanner.nextInt();
switch (num1) {
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
default:
System.out.println("请重新输入!");
break;
}
}
public void Computer(){
num2=(int)(Math.random()%3)+1;
switch (num2){
case 1:
System.out.println(computername+"出拳:剪刀");
break;
case 2:
System.out.println(computername+"出拳:石头");
break;
case 3:
System.out.println(computername+"出拳:布");
break;
}
}
public void showResult(){
System.out.println(computername+"\t"+"vs"+"\t"+peoplename);
System.out.println("对战次数"+count);
System.out.println("姓名"+"\t\t\t"+"得分");
System.out.println(peoplename+"\t\t\t"+score1);
System.out.println(computername+"\t\t\t"+sxore2);
if(score1>=sxore2){
System.out.println("结果:恭喜恭喜");
}else {
System.out.println("结果:呵呵,笨笨,下次加油!");
}
}
}
//第二部分
package com.company;
public class Start {
public static void main(String[] args) {
Game game = new Game();
game.startGame();
}
}
JAVA——人机猜拳
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...