您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“java怎么实现简单猜拳小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“java怎么实现简单猜拳小游戏”吧!
User.java
import java.util.Scanner; public class User { String name; int score; public int showFist(){ System.out.println ("请出拳:1.剪刀\t2.石头\t3.布"); Scanner input=new Scanner ( System.in ); int choice=input.nextInt (); if(choice==1){ System.out.println ("您出了剪刀"); }else if(choice==2){ System.out.println ("您出了石头"); }else if (choice==3){ System.out.println ("您出了布"); }else { System.out.println ("输入有误!"); } return choice; } }
Computer.java
public class Computer { String name; int score; public int showFist () { int choice = (int) (Math.random () * 3) + 1; //产生随机数 if (choice == 1) { System.out.println (name +"出了剪刀" ); } else if (choice == 2) { System.out.println (name +"出了石头" ); } else if (choice == 3) { System.out.println (name +"您出了布" ); } else { System.out.println ( "输入有误!" ); } return choice; } }
Game.java
import java.util.Scanner; public class Game { User user; //用户 Computer computer; //计算机 int count; //记录对战次数 //初始化:设置自己的名字 对手的名字,积分0 public void init(){ System.out.println ("请输入自己的名字"); Scanner input = new Scanner ( System.in ); String name = input.next (); user=new User (); //对象初始化 user.name=name; user.score=0; System.out.println ("请选择你的对手:\n1.张三\t2.李四\t3.王五"); int choice = input.nextInt (); computer=new Computer (); //对象初始化 computer.score=0; switch (choice){ case 1: computer.name="张三"; case 2: computer.name="李四"; case 3: computer.name="王五"; break; default: System.out.println ("输入有误!"); } System.out.println ("你选择与TA对战:"+computer.name); } public void start() { init (); Scanner input=new Scanner ( System.in ); String isContinue = null; do { int userFist = user.showFist (); //人出拳 int chomputerFist = computer.showFist (); //计算机出拳 calcResult ( userFist, chomputerFist ); System.out.println ("是否继续?y(继续)/其他(结束)"); isContinue=input.next (); }while ("y".equals ( isContinue )); showResult (user,computer); //显示最终结果 } //计算每一轮的结果 public void calcResult(int userFist,int computerFist){ //"1.剪刀\t2.石头\t3.布" if((userFist==1&&computerFist==3)||(userFist==2&&computerFist==1)||(userFist==3&&computerFist==2)){ System.out.println ("您赢了"); user.score++; }else if((userFist==3&&computerFist==1)||(userFist==1&&computerFist==2)||(userFist==2&&computerFist==3)){ System.out.println ("您输了"); computer.score++; }else { System.out.println ("您平局"); } } //计算最终结果 public void showResult(User user,Computer computer){ System.out.println (user.name +"\t" +user.score ); System.out.println (computer.name +"\t" +computer.score); if (user.score>computer.score){ System.out.println ("恭喜,获得了最终的胜利"); }else if (user.score<computer.score){ System.out.println ("很遗憾你输了"); }else { System.out.println ("最终平局..."); } } public static void main ( String[] args ) { Game game = new Game (); game.start (); } }
测试结果显示
到此,相信大家对“java怎么实现简单猜拳小游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。