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

标签 java

我正在为我的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("//要检查的首选字符串"))

 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!");
        }

关于java - 为什么我的程序在获取字符串而不是字符时会自行终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799070/

相关文章:

java - NetBeansUpdate 后无法编辑 JFrame 窗体

java - 如何从 hibernate session 中获取 jdbc 连接?

java - Decimalformat:在 JTable 中不需要用逗号替换点

java - 为什么我们不能在 Java 的 main 方法中将字段声明为 public?

java - 请解释一下我的程序的执行顺序

java - 在一个 lambda 表达式中收集复杂对象

java - 如何在具有 Spring Boot 后端的 Vue.js 应用程序中包含 CSRF token ?

java - 如何更正正则表达式以在不区分大小写的情况下找到精确的单词匹配?

Java Stream GroupBy 和 Reduce

java - 将拉绳中的文本居中