人机互动版猜拳游戏用Java怎么做啊?

2024-12-05 00:16:20
推荐回答(2个)
回答1:

这是ACCP第一学期的作业啊,自己多想想,这个给你参考
//用户类
public class user {
int num;
public int showFist(){
Scanner input=new Scanner(System.in);
System.out.print("请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
num=input.nextInt();
switch(num){
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
default:
System.out.println("输入有误!");
}
return num;
}
}
//计算机类
public class computer {
int num1;
public int showFist1(){
num1=(int)(Math.random()*3)+1;
switch(num1){
case 1:
System.out.println("电脑出拳:剪刀");
break;
case 2:
System.out.println("电脑出拳:石头");
break;
case 3:
System.out.println("电脑出拳:布");
break;
}
return num1;
}
}
//游戏类
import java.util.*;
public class game {
Scanner input=new Scanner(System.in);
String person;
String computer;
int count;
int result;
int[] s=new int[]{0,0};
public void initialGame(){
System.out.print("请选择角色(1:刘备 2:孙权 3:曹操):");
int index=input.nextInt();
switch(index){
case 1:
person="刘备";
break;
case 2:
person="孙权";
break;
case 3:
person="曹操";
break;
default:
index=4;
System.out.println("输入有误,请重新输入!");
}
computer="匿名";
count=0;
}
public int calcResult(){
if(s[0]>s[1]){
result=1;
}else if(s[1]>s[0]){
result=2;
}else{
result=3;
}
return result;
}
public void showResult(){
switch(calcResult()){
case 1:
System.out.println("结果:恭喜恭喜!");
break;
case 2:
System.out.println("结果:呵呵,笨笨,下次加油啊!");
break;
case 3:
System.out.println("结果:打成平手,下次再和你一分高下!");
break;
}
}
public void startGame(){
System.out.println("\t\t\t\t---欢迎进入游戏世界---");
System.out.println("\n\n **");
System.out.println(" ** 猜拳,开始 **");
System.out.println(" **");
System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
user u=new user();
computer c=new computer();
game g=new game();
g.startGame();
g.initialGame();
System.out.print("\n要开始吗?(y/n)");
String answer=input.next();
while(answer.equals("y")){
System.out.println();
u.showFist();
c.showFist1();
if((u.num==1&&c.num1==3)||(u.num==2&&c.num1==1)||(u.num==3&&c.num1==2)){
System.out.println("结果:恭喜,你赢了!");
g.s[0]=g.s[0]+1;
}else if((u.num==1&&c.num1==2)||(u.num==2&&c.num1==3)||(u.num==3&&c.num1==1)){
System.out.println("结果:^_^,你输了,真笨!");
g.s[1]=g.s[1]+1;
}else{
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
}
System.out.print("\n\n是否开始下一轮?(y/n):");
answer=input.next();
g.count++;
}
System.out.println("---");//重复代码多了我提交不了,这里自己加长才会好看,
System.out.println(g.person+"\t"+"VS"+"\t"+g.computer );
System.out.println("对战次数:"+g.count);
g.showResult();
}
}

回答2:

你是北大青鸟的吧
package mym3;
public class Mym1{
String min1;
String jihuen1;
String cu1;
String b;
public String a10(){
int random=(int)(Math.random()*3+1);
switch(random){
case 1:
System.out.print("电脑出豢: 剪刀");
b="1";
break;
case 2:
System.out.print("电脑出豢:布");
b="2";
break;
case 3:
System.out.print("电脑出豢:石头");
b="3";
break;
}
return b;
}
}
package mym3;

import java.util.*;

public class Mym0 {
Scanner input = new Scanner(System.in);

public void a11() {
Scanner input = new Scanner(System.in);
int random1 = (int) (Math.random() * 3 + 1);
int index = input.nextInt();
if (random1 == 1 && index != 1) {
System.out.print("电脑是:刘备");
} else if (random1 == 2 && index != 2) {
System.out.print("电脑是:孙权");
} else if (random1 == 3 && index != 3) {
System.out.print("电脑是:曹操");
}
if (index == 1) {
System.out.println(" VS 玩家是: 刘备\n");
} else if (index == 2) {
System.out.println(" VS 玩家是:孙权\n");
} else if (index == 3) {
System.out.println(" VS 玩家是:曹操\n");
}
}

public void a12() {
boolean aaaa = false;
do {
System.out.print("忽略大小写请按《Y/N》!\t");
String substring = input.next();
if (substring.equalsIgnoreCase("Y")) {
System.out.print("要开始吗(Yes/No)?");
String indexOf = input.next();
if (indexOf.equalsIgnoreCase("Yes")) {
System.out.print("\n请出豢:1.剪刀 2.布3.石头(输入相应的数字)\n");
break;
} else if (indexOf.equalsIgnoreCase("No")) {
System.out.print("下次常来玩 ^*^");
break;
} else {
System.out.println("你的输入有误.请重新输入");
aaaa = true;
}
} else {
System.out.print("要开始吗(Yes/No)?");
String indexOf = input.next();
if (indexOf.equals("Yes")) {
System.out.print("\n请出豢:1.剪刀 2.布3.石头(输入相应的数字)");
} else if (indexOf.equals("No")) {
System.out.print("下次常来玩 ^*^");
break;
} else {
System.out.println("你的输入有误.请重新输入");
aaaa = true;
}
}
} while (aaaa);
}
}
还在个 main方法