您的位置:首页 > 编程语言 > Java开发

java 小游戏人机猜拳 关键代码

2016-09-14 15:47 525 查看
<span style="font-size:18px;">import java.util.Scanner;

public class person {
String name;
int score;

public int chuquan() {
Scanner input = new Scanner(System.in);
System.out.println("请输入:1.剪刀2.石头3.布");
System.out.println("请出拳:");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("石头"); ;
break;
case 3:
System.out.println("布");
break;
}
return choice;
}

}
********************
public class comper {
String name;
int score;

public  int chuquan() {
int choice = (int) (Math.random() * 3) + 1;
switch (choice) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("石头"); ;
break;
case 3:
System.out.println("布");
break;
}
return choice;
}}
**********************
import java.util.Scanner;

public class game {
person jia;
comper yi;
int count;

public void initial() {
jia = new person();
yi = new comper();
count = 0;
}

public void qidong() {
initial();
System.out.println("请选择对方英雄:(1:刘备2:孙权3:曹操)");
Scanner input = new Scanner(System.in);
System.out.println("请选择对方人物:");
int num = input.nextInt();
switch (num) {
case 1:
yi.name="刘备";
break;
case 2:
yi.name="孙权";
break;
case 3:
yi.name="曹操";
break;
}
System.out.println("你选择了" + yi.name + "对战!");
System.out.println("要开始吗?(y/n)");
String go = input.next();
System.out.println("请输入你的名字:");
jia.name=input.next();
while ("y".equals(go)) {
int persons = jia.chuquan();
int compers = yi.chuquan();
if ((persons == 1 && compers == 1)
|| (persons == 2 && compers == 2)
|| (persons == 3 && compers == 3)) {
System.out.println("和局!衰!");
} else if ((persons == 1 && compers == 3)
|| (persons == 2 && compers == 1)
|| (persons == 3 && compers == 2)) {
System.out.println("恭喜你 你赢了!");
jia.score++;
} else {
System.out.println("你输了,你真衰!");
yi.score++;
}
count++;
System.out.println("是否开始?(y/n)");
go=input.next();
}
System.out.println("-----------------------------------------");
System.out.println(jia.name+"VS"+yi.name);
System.out.println("对战次数:"+count);
System.out.println("\n姓名:\t得分:");
System.out.println(jia.name+"\t"+jia.score);
System.out.println(yi.name+"\t"+yi.score);
if(jia.score>yi.score){
System.out.println("你最棒!");
}else if(jia.score<yi.score){
System.out.println("你真笨!");
}else{
System.out.println("棋逢对手!");
}

}
}

*****************
import java.util.Scanner;

public class testt {

/**
* @param args
*/
public static void main(String[] args) {
game game=new game();
System.out.println("欢迎进入游戏世界");
System.out.println("**************************");
System.out.println("**猜拳,开始**");
System.out.println("**************************");
game.qidong();
}

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: