今天复习后所输入的代码.
public class ketzy {
public void name() {
}
public static void main() {
System.out.println("----------欢迎进入游戏世界----------\n");
System.out.println(" *****************\n");
System.out.println("\t** 猜拳,开始 **\n");
System.out.println("猜拳规则:1.剪刀 2.石头 3.布");
System.out.println("请输入用户姓名:");
String namu;
System.out.println("请选择对方角色:(1.阿飞 2.李世民 3.诸葛亮):");
Scanner input = new Scanner(System.in);
String chao = "阿飞";
String liou = "诸葛亮";
String shuen = "李世民";
int a = input.nextInt();
if (a == 1) {
System.out.print("你选中的对手是:" + chao);
} else if (a == 2) {
System.out.print("你选中的对手是:" + liou);
} else if (a == 3) {
System.out.print("你选中的对手是:" + shuen);
} else {
System.out.print("你连人都不会选吗,怕不是个傻子!\n");
}
System.out.print("要开始吗?(y/n)");
String con=input.next();
int perFist;
int compFist;
if(con.equals("y")){
perFist=input.nextInt();
compFist=input.nextInt();
if((perFist==1&&compFist==1)||(perFist==2&&compFist==1)||(perFist==3&&compFist==2)){
System.out.println("结果为:平局。\n");
}else if((perFist==1&&compFist==3)||(perFist==2&&compFist==1)||(perFist==3&&compFist==2)){
System.out.println("结果为:你赢了!");
}else{
System.out.println("结果为:你输了哦。");
}
while(){
}
}
}
}
public class ketzy1 {
String name =null;
int score = 0;
public int showFist() {
int show = (int) (Math.random() * 10) % 3 + 1;
switch (show) {
case 1:
System.out.println(name + "出拳: 剪刀");
break;
case 2:
System.out.println(name + "出拳: 石头");
break;
case 3:
System.out.println(name + "出拳: 布");
break;
}
return show;
}
}
public class ketzy2 {
ketzy1 player = new ketzy1();
ketzy1 bot = new ketzy1();
int count1;
public void initial1() {
count = 0;
}
String name =null;
public int showFist() {
int show = (int) (Math.random() * 10) % 3 + 1;
switch (show) {
case 1:
System.out.println(name + "出拳: 剪刀");
break;
case 2:
System.out.println(name + "出拳: 石头");
break;
case 3:
System.out.println(name + "出拳: 布");
break;
}
return show;
}
int count;
public void showo() {
count1 = 0;
}
public void startGame() {
initial1();
Scanner input = new Scanner(System.in);
String con;
int main;
int saite;
System.out.println(" *********************");
System.out.println(" **** 猜拳, 开始 ****");
System.out.println(" *********************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.print("请输入用户名:");
player.name = input.next();
System.out.print("请选择对手(1:孙权 2:刘备 3.曹操):");
main = input.nextInt();
do{
saite = (int) (Math.random() * 10+1);
if (saite == 0) {
bot.name = "你的对手是阿飞";
} else if (saite == 1){
bot.name = "你的对手是诸葛亮";
}else if (saite == 2) {
bot.name = "你的对手是李世民";
}else if (saite == 3) {
System.out.println("已匹配到" + bot.name + "对战");
System.out.print("\n要开始吗?(y/n) ");
con = input.next();
}while(con.equals("n"));
System.out.println(player.name+" VS "+bot.name);
ketzy2 person;
int perFist=person.showFist();
ketzy2 computer;
int compFist=computer.showFist();
while (con.equals("y")) {
perFist = player.showFist();
compFist = bot.showFist();
if ((perFist == 1 && compFist == 1)
|| (perFist == 2 && compFist == 2)
|| (perFist == 3 && compFist == 3)) {
System.out.println("平局");
} else if ((perFist == 1 && compFist == 3)
|| (perFist == 2 && compFist == 1)
|| (perFist == 3 && compFist == 2)) {
System.out.println("胜利");
player.score++;
} else {
System.out.println("败北");
bot.score++;
}
count1++;
System.out.print("\n是否开始下一轮(y/n): ");
con = input.next();
}showResult();
}
public void showResult() {
System.out.println("******对战结束******");
System.out.println(bot.name + " VS " + player.name);
System.out.println("对战次数:" + count1);
System.out.println("你的得分:"+player.score);
System.out.println("对方的得分:"+bot.score);
int result = calcResult();
if (result == 1) {
System.out.println("和局");
} else if (result == 2) {
System.out.println("胜利");
} else {
System.out.println("败北");
}
System.out.println("*********************");
}
public int calcResult() {
if (player.score == bot.score) {
return 1;
} else if (player.score > bot.score) {
return 2;
} else {
return 3;
}
}
}