Paper rock scissors游戏,也许我的代码中缺少了一些东西。

huangapple go评论131阅读模式
英文:

Paper rock scissors game, probably something is missing from my code

问题

  1. public class Game {
  2. public static void main(String[] args) {
  3. String personPlay; // 用户出的手势,可以是 R、P 或 S
  4. String computerPlay = ""; // 计算机出的手势,也是 R、P 或 S
  5. int computerInt; // 随机生成的数字,用来确定计算机的手势
  6. String response;
  7. Scanner scan = new Scanner(System.in);
  8. Random generator = new Random();
  9. System.out.println("让我们来玩剪刀石头布游戏!\n" + "输入你的选择: \n" + "R = 石头, P = 布, S = 剪刀");
  10. System.out.println();
  11. computerInt = generator.nextInt(3);
  12. if (computerInt == 0)
  13. computerPlay = "R";
  14. else if (computerInt == 1)
  15. computerPlay = "P";
  16. else if (computerInt == 2)
  17. computerPlay = "S";
  18. System.out.println("计算机出: " + computerPlay);
  19. System.out.print("输入你的选择: ");
  20. personPlay = scan.next();
  21. if (personPlay.equals(computerPlay))
  22. System.out.println("平局!");
  23. else if (personPlay.equals("R"))
  24. if (computerPlay.equals("S"))
  25. System.out.println("石头砸剪刀! 你赢了!");
  26. else if (personPlay.equals("S"))
  27. if (computerPlay.equals("P"))
  28. System.out.println("剪刀剪布! 你赢了!");
  29. else if (personPlay.equals("P"))
  30. if (computerPlay.equals("R"))
  31. System.out.println("布包石头! 你赢了!");
  32. else if (personPlay.equals("R"))
  33. if (computerPlay.equals("P"))
  34. System.out.println("布包石头! 你输了!");
  35. else if (personPlay.equals("P"))
  36. if (computerPlay.equals("S"))
  37. System.out.println("剪刀剪布! 你输了!");
  38. else if (personPlay.equals("S"))
  39. if (computerPlay.equals("R"))
  40. System.out.println("石头砸剪刀! 你输了!");
  41. else
  42. System.out.println("无效的输入,请重试!");
  43. }
  44. }
英文:

I started learning to program in java two/three days ago and decided to try and program a simple rock paper scissors game.
it wont run and i think that i either didn't include some important code or that this whole thing is just garbage.
thanks for help!

  1. public class Game {
  2. public static void main(String[] args) {
  3. String personPlay; //User's play; either R, P or S
  4. String computerPlay = ""; //Computer's play, R, P or S as well
  5. int computerInt; //randomly generated number to etermine computers play
  6. String response;
  7. Scanner scan = new Scanner(System.in);
  8. Random generator = new Random();
  9. System.out.prinln("Let's play Rock, Paper, Scissors!\n" + "Enter a move: \n" + "R = Rock, P = Paper, S = Scissors");
  10. System.out.println();
  11. computerInt = generator.nextInt(
  12. if (personPlay.equals(computerPlay))
  13. System.out.println("It's a tie!");
  14. else if (personPlay.equals("R"))
  15. if (computerPlay.equals("S"))
  16. System.out.println("Rock crushes Scissors! You win!");
  17. else if (personPlay.equals("S"))
  18. if (computerPlay.equals("P"))
  19. System.out.println("Scissors cut Paper! You win!");
  20. else if (personPlay.equals("P"))
  21. if (computerPlay.equals("R"))
  22. System.out.println("Paper wraps Rock! You win!");
  23. else if (personPlay.equals("R"))
  24. if (computerPlay.equals("P"))
  25. System.out.println("Paper wraps Rock! You lose!");
  26. else if (personPlay.equals("P"))
  27. if (computerPlay.equals("S"))
  28. System.out.println("Scissors cut Paper! You lose!");
  29. else if (personPlay.equals("S"))
  30. if (computerPlay.equals("R"))
  31. System.out.println("Rock crushes Scissors! You lose!");
  32. else System.out.println("Invalid user imput, please try again!");
  33. }
  34. }

答案1

得分: 0

我在你分享的代码中发现了一些错误:

  1. 在语句中,“System.out.prinln("Let's play Rock, Paper, Scissors!\n" + "Enter a move: \n" + "R = Rock, P = Paper, S = Scissors");”,应该是System.out.println()。你有一个拼写错误(在“print”中缺少“t”)。

  2. 你需要初始化变量'personPlay',以使代码编译通过。必须初始化局部变量,因为它们不会假设默认值。

  3. 缺少语句的关闭括号:'computerInt = generator.nextInt('。

修复这些错误可以让代码编译通过。我建议在每个if、else if或else条件中使用花括号,这样代码会更整洁。另外,嵌套的if-else条件链太长,难以阅读和调试(如果出现任何问题)。尽量从简单的条件开始,然后逐步构建你的逻辑,避免出现这么多的if else条件。

英文:

I see a couple of errors in the code that you have shared :

  1. In the statement "System.out.prinln("Let's play Rock, Paper, Scissors!\n" + "Enter a move: \n" + "R = Rock, P = Paper, S = Scissors");"
    , it should be System.out.println().You have a spelling-error (missing 't' in 'print')

  2. You need to initialize variable 'personPlay' for the code to compile. It is required to initialize local variables as they do not assume a default value.

  3. Missing closing bracket for statement :'computerInt = generator.nextInt('.

Fixing these, would let the code compile .
I would suggest using curly braces with each if, else if or else conditions, that would give the code a neat look. Also , there is a long chain of nested if-else conditions which is hard to read and debug(in case of any issue). Try to start with simple conditions and then build your logic in steps , avoiding these many if else conditions.

答案2

得分: 0

我相信这回答了你的问题:

  1. import java.util.Scanner;
  2. public class Game {
  3. public static void main(String[] args) {
  4. String personPlay; // 用户的选择;可以是 R、P 或 S
  5. String computerPlay = ""; // 电脑的选择,也是 R、P 或 S
  6. String response;
  7. Scanner scan = new Scanner(System.in);
  8. System.out.println("让我们玩剪刀石头布游戏!\n" + "输入一个选项:\n" + "R = 石头, P = 布, S = 剪刀");
  9. System.out.print("玩家 > ");
  10. personPlay = scan.nextLine();
  11. System.out.print("电脑 > ");
  12. String options[] = {"R", "P", "S"};
  13. computerPlay = options[(int) (Math.random() * 3)];
  14. System.out.println(computerPlay);
  15. if (personPlay.equals(computerPlay)) {
  16. System.out.println("平局!");
  17. } else if (personPlay.equals("R")) {
  18. if (computerPlay.equals("P")) {
  19. System.out.println("布包裹石头!你输了!");
  20. } else if (computerPlay.equals("S")) {
  21. System.out.println("石头粉碎剪刀!你赢了!");
  22. }
  23. } else if (personPlay.equals("P")) {
  24. if (computerPlay.equals("R")) {
  25. System.out.println("布包裹石头!你赢了!");
  26. } else if (computerPlay.equals("S")) {
  27. System.out.println("剪刀切割布!你输了!");
  28. }
  29. } else if (personPlay.equals("S")) {
  30. if (computerPlay.equals("R")) {
  31. System.out.println("石头粉碎剪刀!你输了!");
  32. } else if (computerPlay.equals("P")) {
  33. System.out.println("剪刀切割布!你赢了!");
  34. }
  35. } else {
  36. System.out.println("无效的用户输入,请重试!");
  37. }
  38. }
  39. }

你的代码存在一些问题,其中大部分已经被其他用户指出。我只想补充一下你编写的 if/else 语句,有一些分支永远不会被执行。我现在时间有限,如果在阅读这个答案后你仍有疑问,请留下评论,我会尽快回复。

英文:

I believe this answers your question:

  1. import java.util.Scanner;
  2. public class Game {
  3. public static void main(String[] args) {
  4. String personPlay; //User's play; either R, P or S
  5. String computerPlay = ""; //Computer's play, R, P or S as well
  6. String response;
  7. Scanner scan = new Scanner(System.in);
  8. System.out.println("Let's play Rock, Paper, Scissors!\n" + "Enter a move: \n" + "R = Rock, P = Paper, S = Scissors");
  9. System.out.print("Player > ");
  10. personPlay = scan.nextLine();
  11. System.out.print("Computer > ");
  12. String options [] = {"R","P","S"};
  13. computerPlay = options[(int)(Math.random()*3)];
  14. System.out.println(computerPlay);
  15. if (personPlay.equals(computerPlay)) {
  16. System.out.println("It's a tie!");
  17. }
  18. else if (personPlay.equals("R")) {
  19. if (computerPlay.equals("P")) {
  20. System.out.println("Paper wraps Rock! You lose!");
  21. }
  22. else if (computerPlay.equals("S")) {
  23. System.out.println("Rock crushes Scissors! You win!");
  24. }
  25. }
  26. else if (personPlay.equals("P")) {
  27. if (computerPlay.equals("R")) {
  28. System.out.println("Paper wraps Rock! You win!");
  29. }
  30. else if (computerPlay.equals("S")) {
  31. System.out.println("Scissors cut Paper! You lose!");
  32. }
  33. }
  34. else if (personPlay.equals("S")) {
  35. if (computerPlay.equals("R")) {
  36. System.out.println("Rock crushes Scissors! You lose!");
  37. }
  38. else if (computerPlay.equals("P")) {
  39. System.out.println("Scissors cut Paper! You win!");
  40. }
  41. }
  42. else { System.out.println("Invalid user imput, please try again!"); }
  43. }
  44. }

There were a few problems with your code, most of them already pointed by other users. I'll only add that the way you wrote those if/else statements, it would never enter some of them.
I don't have much time right now but if you still have any doubts after seeing this answer leave a comment and I'll answer ASAP.

huangapple
  • 本文由 发表于 2020年9月10日 16:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63825485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定