第三章第十七题(游戏:剪刀、石头、布)(Game: scissor, rock, paper)

*3.17(游戏:剪刀、石头、布)编写可以玩流行的剪刀-石头-布游戏的程序。(剪刀可以剪布,石头可以砸剪刀,而布可以包石头。)程序提示用户随机产生一个数,这个数为0、1或者2,分别表示石头、剪刀和布。程序提示用户输入值0、1或者2,然后显示一条消息,表明用户和计算机谁赢了游戏,谁输了游戏,或是打成平手。

下面是运行示例:
scissor(0),rock(1),paper(2): 1
The computer is scissor.You are rock.You won
scissor(0),rock(1),paper(2): 2
The computer is paper.You are paper too.It is a draw

*3.17(Game: scissor, rock, paper) Write a program that plays the popular scissor–rock–paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.

Here are sample runs:
scissor(0),rock(1),paper(2): 1
The computer is scissor.You are rock.You won
scissor(0),rock(1),paper(2): 2
The computer is paper.You are paper too.It is a draw

下面是参考答案代码:

import java.util.*;

public class ScissorRockPaperQuestion17 {
   
	public static void main(String[] args) {
   
		// Generate the computer's guess
		final int userGuess,computerGuess = (int)(Math.random()*3);
		
		// Prompt the user to enter a guess
		System.out.print("scissor(0),rock(1),paper(2): ");
		Scanner input = new Scanner(System
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值