四.public class Test{ public static void main(String[] args){ Game game=new Game(); game.initial(); game.startGame();} }
三.public class game{Person person;Computer=computerint count ;public void initial(){person=new Person();computer=new Computer();count=0;}public void startgame(){System.out.println("欢迎进入猜拳");System.out.println("********");System.out.println("**猜拳开始**");System.out.println("********");System.out.prontln("出拳规则:1.剪刀2.石头3.布");Scanner input=new Scanner(System.in);System.out.println("请选择对方角色:(1.刘备2.曹操3.孙权)");int role=input.nextInt();System.out.println("请输入你的名字");person. name=input.next();switch(role){case 1:computer.name="刘备";break;case 2:computer.name="孙权"break;case 3:computer.name="曹操"break;}System.out.println(computer.name+"vs"+person.name+"\n")System.out.println("是否开始游戏(y/n)");String con=input.next();while(con.equals("y")){int pershow=person.showfist();int comshow=computer.showfist();if(pershow==comshow){System.out.println("真衰,平局");}else if((pershow==1&&comshow==2)||(pershow==2&&comshow==3)||(pershow==3&&comshow==1)){System.out.println("真悲哀,你输了");computer.score++;count++;}else if(pershow==2&&comshow==1||pershow==3&&show==2||pershow==1&&comshow==3){System.out.println("恭喜你你赢了!");person.score++;count++;}System.out.println("是否开始下一轮?y/n");con=input.next();}System.out.println("对战次数"+count);System.out.println("computer.name+"vs"+person.name");System.out.println("computer.score+"vs"+person.name");if(computer.score>person.score){System.out.println("你输了");}else(computer.score==person.score){System.out.println("平局");}else{System.out.println("你赢了");}} }一.public class computer{ String name="电脑"; int score=0; public int showfist(){ int show=(int)(Math.random()*10)%3+1 switch(show){ case 1: System.out.println("电脑出拳:剪刀"); break; case 2: System.out.println("电脑出拳:石头"); break; case 3: System.out.println(电脑出拳:布); break;} return show; }} 二public class person{ String name="匿名"; int score=0; public int showfist(){ System.out.println("请猜拳(1.剪刀,2.石头,3.布(请选择))"); Scanner input=new Scanner(System.in) int show=input.nextInt(); switch(show){ case 1:
System.out.println("你出:剪刀"); break; case 2: System.out.println("你出:石头"); break; case 3: System.out.println("你出:布"); break;} return show;}}
人机猜拳(顺序:一-->二-->三-->四)
最新推荐文章于 2021-02-25 07:57:20 发布