永发信息网

使用Java实现决策器

答案:2  悬赏:30  手机版
解决时间 2021-05-25 21:07
–java编写:猜拳游戏,由两个用户输入自己的选择,彼此不能知道,并且判断输赢。不要求界面
最佳答案

我有个人机对战的,你改改就行了!


package com.game.guess;


public class Computer {
String name1="匿名";
int score;
public int showFist(){
int num2=(int)(Math.random()*3+1);

switch(num2){
case 1:
System.out.println("电脑出拳:剪刀");
break;
case 2:
System.out.println("电脑出拳:石头");
break;
case 3:
System.out.println("电脑出拳:布");
break;


}
return num2;
}


}


package com.game.guess;


import java.util.Scanner;


public class Game {
Person person;
Computer computer;
int count;


public void chushihua(){
person=new Person();
computer=new Computer();
count =0;
}

public int calcrerult(){
if(person.score>computer.score){
return 1;
}else if(person.score<computer.score){
return 2;
}else {
return 0;
}
}

public void showRerult(){
System.out.println("-----------------------------------------");
System.out.println(person.name+"VS"+computer.name1);
System.out.println("对战次数:"+count);
int result=calcrerult();
switch(result){
case 0:
System.out.println("和局,真衰!嘿嘿等着瞧吧!");
break;
case 1:
System.out.println("结果:恭喜,你赢了!");
break;
case 2:
System.out.println("结果:^-^,你输了,真笨!");
break;
}
System.out.println("-----------------------------------------");
}

public void startGame(){
System.out.println("------------------欢迎进入游戏世界----------------");
System.out.println("\t\t****************");
System.out.println("\t\t **猜拳,开始**");
System.out.println("\t\t****************\n\n");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.print("请选择角色(1.刘备 2.孙权 3.曹操):");
Scanner in=new Scanner(System.in);
int men=in.nextInt();
switch(men){
case 1:
person.name="刘备";
break;
case 2:
person.name="孙权";
break;
case 3:
person.name="曹操";
break;


}


System.out.println();
System.out.print("要开始吗? (Y/N)");
String answer=in.next();
for(;;) {
System.out.println("请出拳:1.剪刀 2.石头 3.布");
chushihua();
int user=person.showFist();
int user1=computer.showFist();
count++;
if((user==1&&user1==3)||(user==2&&user1==2)||(user==3&&user1==1)){
person.score++;
System.out.println("结果:恭喜,你赢了!");
}else if(user==user1){
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}else{
computer.score++;
System.out.println("结果:嘿嘿,你输了,真笨!");
}

System.out.println("\n\n是否开始下一轮(y/n)");
answer=in.next();

if(answer.equals("y")){
continue;
}else{
break;
}
}


showRerult();
}

}
package com.game.guess;


import java.util.Scanner;


public class Person {
public String name;
int score;
public int showFist(){
Scanner in=new Scanner(System.in);

int num1=in.nextInt();
switch(num1){
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
default:

}
return num1;
}


}
package com.game.guess;


public class Test {
public static void main(String[] args) {
Game game=new Game();
game.chushihua();
game.startGame();


}
}


全部回答
import java.util.HashMap; import java.util.Scanner; public class Puzzle { private final static int ROCK = 0; private final static int SCISSOR = 1; private final static int PAPER = 2; private static HashMap<Integer, String> choiceName; public static void init() { choiceName = new HashMap<Integer, String>(); choiceName.put(ROCK, "石头"); choiceName.put(SCISSOR, "剪子"); choiceName.put(PAPER, "布"); } private static void clear() { for (int i = 0; i < 50; i++) System.out.println(); } private static int input(int n, Scanner scanner) { int ch = 0; while (true) { System.out.printf("请用户%d选择 (0-石头 1-剪子 2-布)", n); String in = scanner.next(); try { ch = Integer.parseInt(in); break; } catch (NumberFormatException ex) { System.out.println("输入错误,重新输入"); } } return ch; } public static void start() { init(); Scanner scanner = new Scanner(System.in); int[] choices = new int[2]; int[] win = new int[2]; int round = 0; while (true) { for (int i = 0; i < 2; i++) { clear(); choices[i] = input(i + 1, scanner); } System.out.printf("用户1选择的是%s\n", choiceName.get(choices[0])); System.out.printf("用户2选择的是%s\n", choiceName.get(choices[1])); int result = choices[0] - choices[1]; switch (result) { case 0: System.out.println("平"); break; case -1: case 2: System.out.println("用户1胜"); ++win[0]; break; case 1: case -2: System.out.println("用户2胜"); ++win[1]; break; } ++round; System.out.print("y键继续,其他键结束\n"); String cont = scanner.next(); if (!cont.equalsIgnoreCase("y")) { System.out.printf("一共进行了%d局\n用户1胜%d,负%d,平%d\n用户2胜%d,负%d,平%d", round, win[0], win[1], round - win[0] - win[1], win[1], win[0], round - win[0] - win[1]); break; } } } public static void main(String[] args) { start(); } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
诛仙飞升135+8装备挂洞明细
黄金·时代广场E座地址在哪,我要去那里办事
番茄酱做饭怎么做好吃,番茄酱辣年糕的做法,
孝南区孝感大拇指巴黎春天幼儿园这个地址在什
解释一下“效率就是明智的懒惰”。
单跑楼梯计算投影面积时宽度要加楼梯井宽度么
仙桃市仙桃京兰水泥(毛场店)我想知道这个在什
兰考县开封香香蛋糕哪位知道具体地址啊
基地草莓采摘园怎么去啊,有知道地址的么
手机通话键不能用了
雨花区招商银行(长沙左家塘支行)在什么地方啊
1988年农历十月十四是什么星座
最近在网上看到GBAz5的视频,难道zero出5了?
最好在纸上
南岗区哈尔滨海洋洁面健康养生馆这个地址怎么
推荐资讯
网络里有真情感存在吗?
炫舞舞团如何管理?
大家对八零后的有什么看法与评价?
QQ华夏神战秘法盾
SONY笔记本装WIN7旗舰版系统
手机的格式化
谁有杨丞琳雨爱完整版
年底要做点什么?
哪一种瓶装绿茶比较好喝?
为什么重装系统会发生这样的问题
孵化人员需要技师证吗
天书奇谈40历练
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?