Java 为什么我的程序在获取字符串而不是字符时会自动终止?

Java 为什么我的程序在获取字符串而不是字符时会自动终止?,java,Java,我正在为我的java类制作一个石头剪刀的游戏,但是当我要求它接受一个字符串时,程序运行后就终止了。我以前将其设置为使用字符,每次都有效。我哪里出错了 另外,如果你不介意告诉我如何插入重播选项,那就太好了 import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner input = new Scanner(System.in);

我正在为我的java类制作一个石头剪刀的游戏,但是当我要求它接受一个字符串时,程序运行后就终止了。我以前将其设置为使用字符,每次都有效。我哪里出错了

另外,如果你不介意告诉我如何插入重播选项,那就太好了

import java.util.Scanner;


public class RockPaperScissors {

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);

    System.out.println("Choose your hand (rock, paper, scissors, lizard, Spock):");

    String hand = input.nextLine().toUpperCase();

    int opponent = (int)(Math.random() * ((4) + 1));


    if (hand == "ROCK") {
        if (opponent == 0 ) {
        System.out.println("Rock does nothing to rock. Tie.");
        }   
        else if (opponent == 1) {
            System.out.println("Paper covers rock. You Lose...");
        }
        else if (opponent == 2) {
            System.out.println("Rock smashes scissors. You Win! ");
        }
        else if (opponent == 3) {
            System.out.println("Rock smashes lizard. You Win!");
        }
        else  {
            System.out.println("Spock vaporizes rock. You Lose...");
        }
    }


    if (hand == "PAPER") {
        if (opponent == 0 ) {
            System.out.println("Paper covers rock. You Win!");
        }   
        else if (opponent == 1) {
                System.out.println("paper does nothing to paper. Tie.");
        }
        else if (opponent == 2) {
            System.out.println("Scissors cuts paper. You Lose...");
        }
        else if (opponent == 3) {
            System.out.println("Lizard eats paper. You Lose...");
        }
        else  {
            System.out.println("Paper disproves Spock. You Win!");
        }
    }

        if (hand == "SCISSORS") {
            if (opponent == 0 ) {
                System.out.println("Rock smashes Scissors. You Lose...");
            }   
            else if (opponent == 1) {
                    System.out.println("Scissors cuts paper. You Win!");
            }
            else if (opponent == 2) {
                System.out.println("Scissors do nothing to scissors. Tie.");
            }
            else if (opponent == 3) {
                System.out.println("Scissors decapitates lizard. You Win!");
            }
            else {
                System.out.println("Spock smashes scissors. You Lose...");
            }
        }

            if (hand == "LIZARD") {
                if (opponent == 0 ) {
                    System.out.println("Rock smashes lizard. You Lose...");
                }   
                else if (opponent == 1) {
                        System.out.println("Lizard eats paper. You Win!");
                }
                else if (opponent == 2) {
                    System.out.println("Scissors decapitates lizard. You Lose...");
                }
                else if (opponent == 3) {
                    System.out.println("Lizard does nothing to lizard. Tie.");
                }
                else {
                    System.out.println("Lizard poisons Spock. You Win!");
                }
            }

                if (hand == "SPOCK" ) {
                    if (opponent == 0 ) {
                        System.out.println("Spock vaporizes rock. You Win!");
                    }   
                    else if (opponent == 1) {
                            System.out.println("Paper disproves Spock. You Lose...");
                    }
                    else if (opponent == 2) {
                        System.out.println(" Spock smashes scissors. You Win!");
                    }
                    else if (opponent == 3) {
                        System.out.println("Lizard poisons Spock. You Lose...");
                    }
                    else  {
                        System.out.println("Spock does nothing to Spock. Tie");
                    }
                if (hand != "ROCK" && hand != "PAPER" && hand != "SCISSORS" && hand != "LIZARD" && hand != "SPOCK") {
                    System.out.println("Invalid hand.");
                }       
        }           
    }
}
试试这个

 if (hand == "PAPER") {
        if (opponent == 0 ) {
            System.out.println("Paper covers rock. You Win!");
        }   
        else if (opponent == 1) {
                System.out.println("paper does nothing to paper. Tie.");
        }
        else if (opponent == 2) {
            System.out.println("Scissors cuts paper. You Lose...");
        }
        else if (opponent == 3) {
            System.out.println("Lizard eats paper. You Lose...");
        }
        else  {
            System.out.println("Paper disproves Spock. You Win!");
        }
像这样改变
if(hand.equals(“//要检查的首选字符串”))


@哈格博伊:你能用问题添加堆栈跟踪(错误)吗。这将有助于解决你的问题。谢谢你把它也放进去了。没有变化。是的解决我想知道你的错误。请添加堆栈跟踪(控制台/终端上的错误)没有显示错误。Tim链接的问题解决了它。我比较的方式是错误的。谢谢你的帮助。
 if (hand.equals("PAPER")) {
        if (opponent == 0 ) {
            System.out.println("Paper covers rock. You Win!");
        }   
        else if (opponent == 1) {
                System.out.println("paper does nothing to paper. Tie.");
        }
        else if (opponent == 2) {
            System.out.println("Scissors cuts paper. You Lose...");
        }
        else if (opponent == 3) {
            System.out.println("Lizard eats paper. You Lose...");
        }
        else  {
            System.out.println("Paper disproves Spock. You Win!");
        }