第三章第十七题(游戏:剪刀、石头、布)(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.in);
		userGuess = input.nextInt();
		
		// Check user's guess and Display the result
		if(computerGuess == 0)	// The situation that computer guess is a scissor
		{
			if(userGuess == 0)
			{
				System.out.print("The computer is scissor.");
				System.out.print("You are scissor too.");
				System.out.print("It is a draw");
			}
			else if(userGuess == 1)
			{
				System.out.print("The computer is scissor.");
				System.out.print("You are rock.");
				System.out.print("You won");
			}
			else if(userGuess == 2)
			{
				System.out.print("The computer is scissor.");
				System.out.print("You are paper.");
				System.out.print("You lost");
			}
		}
		else if(computerGuess == 1) // The situation that computer guess is a rock
		{
			if(userGuess == 0)
			{
				System.out.print("The computer is rock.");
				System.out.print("You are scissor.");
				System.out.print("You lost");
			}
			else if(userGuess == 1)
			{
				System.out.print("The computer is rock.");
				System.out.print("You are rock too.");
				System.out.print("It is a draw");
			}
			else if(userGuess == 2)
			{
				System.out.print("The computer is rock.");
				System.out.print("You are paper.");
				System.out.print("You won");
			}
		}
		else //The situation that computer guess is a paper
		{
			if(userGuess == 0)
			{
				System.out.print("The computer is paper.");
				System.out.print("You are scissor.");
				System.out.print("You won");
			}
			else if(userGuess == 1)
			{
				System.out.print("The computer is paper.");
				System.out.print("You are rock.");
				System.out.print("You lost");
			}
			else if(userGuess == 2)
			{
				System.out.print("The computer is paper.");
				System.out.print("You are paper too.");
				System.out.print("It is a draw");
			}
		}
		
		// Display invalid situation
		if(userGuess > 2 || userGuess < 0)
		{
			System.out.println("Error:Invalid Guess");
			System.exit(1);
		}
		input.close();
	}
}

运行效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)

你可能感兴趣的:(#,第三章课后习题答案,java,游戏,代码规范,小程序)