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

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

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

问题

public class Game {
    public static void main(String[] args) {
        String personPlay; // 用户出的手势,可以是 R、P 或 S
        String computerPlay = ""; // 计算机出的手势,也是 R、P 或 S
        int computerInt; // 随机生成的数字,用来确定计算机的手势
        String response;

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();

        System.out.println("让我们来玩剪刀石头布游戏!\n" + "输入你的选择: \n" + "R = 石头, P = 布, S = 剪刀");

        System.out.println();

        computerInt = generator.nextInt(3);

        if (computerInt == 0)
            computerPlay = "R";
        else if (computerInt == 1)
            computerPlay = "P";
        else if (computerInt == 2)
            computerPlay = "S";

        System.out.println("计算机出: " + computerPlay);

        System.out.print("输入你的选择: ");
        personPlay = scan.next();

        if (personPlay.equals(computerPlay))
            System.out.println("平局!");
        else if (personPlay.equals("R"))
            if (computerPlay.equals("S"))
                System.out.println("石头砸剪刀! 你赢了!");
        else if (personPlay.equals("S"))
            if (computerPlay.equals("P"))
                System.out.println("剪刀剪布! 你赢了!");
        else if (personPlay.equals("P"))
            if (computerPlay.equals("R"))
                System.out.println("布包石头! 你赢了!");
        else if (personPlay.equals("R"))
            if (computerPlay.equals("P"))
                System.out.println("布包石头! 你输了!");
        else if (personPlay.equals("P"))
            if (computerPlay.equals("S"))
                System.out.println("剪刀剪布! 你输了!");
        else if (personPlay.equals("S"))
            if (computerPlay.equals("R"))
                System.out.println("石头砸剪刀! 你输了!");
        else
            System.out.println("无效的输入,请重试!");
    }
}
英文:

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!

    public class Game {
	public static void main(String[] args) {
		String personPlay; //User's play; either R, P or S
		String computerPlay = ""; //Computer's play, R, P or S as well
		int computerInt; //randomly generated number to etermine computers play
		String response;
		
		Scanner scan = new Scanner(System.in);
		Random generator = new Random();
		
		System.out.prinln("Let's play Rock, Paper, Scissors!\n" + "Enter a move: \n" + "R = Rock, P = Paper, S = Scissors");
		
		System.out.println();
		
		computerInt = generator.nextInt(
		
		
		if (personPlay.equals(computerPlay))
			System.out.println("It's a tie!");
		else if (personPlay.equals("R"))
			if (computerPlay.equals("S"))
				System.out.println("Rock crushes Scissors! You win!");
	    else if (personPlay.equals("S"))
	        if (computerPlay.equals("P"))
	            System.out.println("Scissors cut Paper! You win!");
	   else if (personPlay.equals("P"))
	        if (computerPlay.equals("R"))
                System.out.println("Paper wraps Rock! You win!");
        else if (personPlay.equals("R"))
            if (computerPlay.equals("P"))
                System.out.println("Paper wraps Rock! You lose!");
         else if (personPlay.equals("P"))
            if (computerPlay.equals("S"))
                System.out.println("Scissors cut Paper! You lose!");
         else if (personPlay.equals("S"))
            if (computerPlay.equals("R"))
                System.out.println("Rock crushes Scissors! You lose!");
        else    System.out.println("Invalid user imput, please try again!");
	}
}

答案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

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

import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        String personPlay; // 用户的选择;可以是 R、P 或 S
        String computerPlay = ""; // 电脑的选择,也是 R、P 或 S
        String response;

        Scanner scan = new Scanner(System.in);

        System.out.println("让我们玩剪刀石头布游戏!\n" + "输入一个选项:\n" + "R = 石头, P = 布, S = 剪刀");
        System.out.print("玩家 > ");
        personPlay = scan.nextLine();

        System.out.print("电脑 > ");
        String options[] = {"R", "P", "S"};
        computerPlay = options[(int) (Math.random() * 3)];
        System.out.println(computerPlay);

        if (personPlay.equals(computerPlay)) {
            System.out.println("平局!");
        } else if (personPlay.equals("R")) {
            if (computerPlay.equals("P")) {
                System.out.println("布包裹石头!你输了!");
            } else if (computerPlay.equals("S")) {
                System.out.println("石头粉碎剪刀!你赢了!");
            }
        } else if (personPlay.equals("P")) {
            if (computerPlay.equals("R")) {
                System.out.println("布包裹石头!你赢了!");
            } else if (computerPlay.equals("S")) {
                System.out.println("剪刀切割布!你输了!");
            }
        } else if (personPlay.equals("S")) {
            if (computerPlay.equals("R")) {
                System.out.println("石头粉碎剪刀!你输了!");
            } else if (computerPlay.equals("P")) {
                System.out.println("剪刀切割布!你赢了!");
            }
        } else {
            System.out.println("无效的用户输入,请重试!");
        }
    }
}

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

英文:

I believe this answers your question:

import java.util.Scanner;
public class Game {
public static void main(String[] args) {
String personPlay; //User's play; either R, P or S
String computerPlay = ""; //Computer's play, R, P or S as well
String response;
Scanner scan = new Scanner(System.in);
System.out.println("Let's play Rock, Paper, Scissors!\n" + "Enter a move: \n" + "R = Rock, P = Paper, S = Scissors");
System.out.print("Player > ");
personPlay = scan.nextLine();
System.out.print("Computer > ");
String options [] = {"R","P","S"};
computerPlay = options[(int)(Math.random()*3)];
System.out.println(computerPlay);
if (personPlay.equals(computerPlay)) {
System.out.println("It's a tie!");
}
else if (personPlay.equals("R")) {
if (computerPlay.equals("P")) {
System.out.println("Paper wraps Rock! You lose!");
}
else if (computerPlay.equals("S")) {
System.out.println("Rock crushes Scissors! You win!");
}
}
else if (personPlay.equals("P")) {
if (computerPlay.equals("R")) { 
System.out.println("Paper wraps Rock! You win!");
}
else if (computerPlay.equals("S")) {
System.out.println("Scissors cut Paper! You lose!");
}
}
else if (personPlay.equals("S")) {
if (computerPlay.equals("R")) {
System.out.println("Rock crushes Scissors! You lose!");
}
else if (computerPlay.equals("P")) {
System.out.println("Scissors cut Paper! You win!");
}
}
else { System.out.println("Invalid user imput, please try again!"); }
}
}

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:

确定