首页 > 解决方案 > 为什么我的程序在获取字符串而不是字符时会自行终止?

问题描述

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

PS如果您不介意向我展示如何再次插入播放选项,那就太好了。

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

标签: java

解决方案


尝试这个

 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("//Preferred String to Check"))

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

推荐阅读