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

    科技2022-07-12  142

    第三章第十七题(游戏:剪刀、石头、布)(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

    参考代码:

    package chapter03; import java.util.Scanner; public class Code_17 { 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(); } } 结果显示: scissor(0),rock(1),paper(2): 2 The computer is rock.You are paper.You won Process finished with exit code 0
    Processed: 0.010, SQL: 8