这是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();
}
}
温馨提示:答案为网友推荐,仅供参考