用Java编写的猜拳小游戏

学习目标:

熟练掌握各种循环语句

例题:

代码如下:

// 综合案例分析,猜拳案例
// isContinue为是否开始游戏时你所输入的值
char isContinue;
//y为开始,n为借宿
System.out.println("是否开始游戏(y/n)");
Scanner sc = new Scanner(System.in);
String str = sc.next();
// 获取你输入字符串的第一个字符
isContinue = str.charAt(0);
// mcount代表玩家赢的局数,pcount代表电脑赢的局数
int mcount = 0, pcount = 0;

//你的名字
System.out.println("请输入您的名字");
String pName = sc.next();
System.out.println("您的名字是:" + pName);

System.out.println("请选择您的对手:1、貂蝉, 2、小肥羊,3、吕布");
// cpuName电脑名字
String cpuName = null;
int num3 = sc.nextInt();
if(num3 >= 1 && num3 <= 3) {
   
	switch (num3
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用Eclipse编写Java猜拳小游戏的步骤: 1. 创建新的Java工程,命名为RockPaperScissors。 2. 在src文件夹下创建一个名为com的package,然后在com包下创建一个名为rps的package。 3. 在rps包下创建一个名为Game的Java类,这个类将包含猜拳游戏的主要逻辑。 4. 在Game类中添加以下成员变量: ``` private int playerScore; private int computerScore; private Scanner scanner; ``` 这些变量将分别用于保存玩家和计算机的得分,并用于读取用户输入。 5. 在Game类中添加以下构造函数: ``` public Game() { playerScore = 0; computerScore = 0; scanner = new Scanner(System.in); } ``` 这个构造函数将用于初始化成员变量。 6. 在Game类中添加以下方法: ``` public void run() { System.out.println("Welcome to Rock-Paper-Scissors!"); while (true) { System.out.println("Player Score: " + playerScore); System.out.println("Computer Score: " + computerScore); System.out.println("Choose your move (1 = rock, 2 = paper, 3 = scissors):"); int playerMove = scanner.nextInt(); int computerMove = (int) (Math.random() * 3) + 1; System.out.println("Computer chose: " + computerMove); int result = getResult(playerMove, computerMove); if (result == 0) { System.out.println("Tie!"); } else if (result == 1) { System.out.println("You win!"); playerScore++; } else { System.out.println("You lose!"); computerScore++; } } } private int getResult(int playerMove, int computerMove) { if (playerMove == computerMove) { return 0; } else if ((playerMove == 1 && computerMove == 3) || (playerMove == 2 && computerMove == 1) || (playerMove == 3 && computerMove == 2)) { return 1; } else { return -1; } } ``` 这些方法将用于运行游戏并计算结果。 7. 在Game类中添加以下main方法: ``` public static void main(String[] args) { Game game = new Game(); game.run(); } ``` 这个main方法将用于启动游戏。 8. 运行程序,开始玩猜拳游戏! 希望这个步骤能够帮助你完成用Eclipse编写Java猜拳小游戏的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值