字符串输入在if-else语句中不起作用

时间:2013-06-22 09:41:08

标签: java string

我是Java的新手,我创建了一些迷你游戏,我希望玩家选择是否想要再次玩游戏,我尝试将我的开始布尔变量更改为静态类型并添加一些行代码,但代码似乎不起作用,每次我玩游戏,它确实问我是否要重播,但问题是,即使我最后在控制台输入“是”,游戏似乎没有重启。任何人都可以帮助我,我真的很感激,谢谢!

import java.util.Random;
import java.util.Scanner;

//Rocks, papers, scissors game complied by William To.
public class RockPaperScissor {
static int gamePlays = 0;
static boolean start = true;
public static void main(String[] args) {
    while (start){
        @SuppressWarnings("resource")
        Scanner userInput = new Scanner(System.in);
        Random comOutput = new Random();
        System.out.println("Do you choose rock, paper or scissor?");
        String userChoice = userInput.nextLine();
        int comChoice = comOutput.nextInt(2);
        switch (userChoice){

        case "rock":
            if(comChoice == 0){
                System.out.println("I choose rock too! That's a draw!");
            } else if(comChoice == 1){
                System.out.println("I choose paper! I win!");
            } else {
                System.out.println("I choose scissor! You win!");
            }
            break;

        case "paper":
            if(comChoice == 0){
                System.out.println("I choose rock! You win!");
            } else if(comChoice == 1){
                System.out.println("I choose paper too! That's a draw!");
            } else {
                System.out.println("I choose scissor! I win!");
            }
            break;

        case "scissor":
            if(comChoice == 0){
                System.out.println("I choose rock! I win!");
            } else if(comChoice == 1){
                System.out.println("I choose paper! You win!");
            } else {
                System.out.println("I choose scissor too! That's a draw!");
            }
            break;
        default :
            System.out.println("I don't think that's an option.");
            break;
        }
        gamePlays++;
        System.out.println("You've played " + gamePlays + " games.");

               //BELOW IS THE PART I want to ask, what's wrong? 

        System.out.println("Do you want to play again?"); 
        String playAgain = userInput.next();
        if (playAgain == "yes"){
            System.out.println("Restarting game...");
            start = true;
        } else if (playAgain == "no") {
            System.out.println("Quitting game.");
            start = false;
        }
    }
}   
} 

从Eclipse打印出来:

Do you choose rock, paper or scissor?  
rock **//my input**    
I choose paper! I win!  
You've played 1 games.  
Do you want to play again?    
yes **//my input**    
Do you choose rock, paper or scissor?  
paper **//my input**    
I choose paper too! That's a draw!  
You've played 2 games.  
Do you want to play again?  
no **//my input**  
Do you choose rock, paper or scissor? 

5 个答案:

答案 0 :(得分:4)

尝试:

    if ("yes".equals(playAgain)) {
        System.out.println("Restarting game...");
        start = true;
    } else if ("no".equals(playAgain)) {
        System.out.println("Quitting game.");
        start = false;
    }

String#equals()检查字符串的字符相等性,==检查内存引用。

答案 1 :(得分:3)

字符串之间的比较需要String#equals而不是==。所以这个:

if (playAgain == "yes"){
    System.out.println("Restarting game...");
    start = true;
} else if (playAgain == "no") {
    System.out.println("Quitting game.");
    start = false;
}

必须成为这个:

if (playAgain.equals("yes")){
    System.out.println("Restarting game...");
    start = true;
} else if (playAgain.equals("no")) {
    System.out.println("Quitting game.");
    start = false;
}

为了正常工作。

但是,我建议使用String#equalsIgnoreCase,因为对于equals,比较区分大小写,因此如果用户输入即YesyEs,{{1 }}将返回playAgain.equals("yes")

<强>更新

正如Maroun Maroun所说,最好使用

false

"yes".equals(playAgain)

因为如果playAgain为"no".equals(playAgain) ,则不会抛出null

答案 2 :(得分:3)

问题可能是这一行

if (playAgain == "yes")

(之后的那个没有)。

Java中的字符串是对象。这意味着两个字符串可能具有相同的内容,即包含相同的文本,但就==而言仍然不是“相同”。要比较两个字符串并查看它们是否包含相同的文本,请使用:

if (playAgain.equals("yes"))

答案 3 :(得分:1)

问题在于String比较的方式。

playAgain == "yes",此检查将检查对象是否相等。

但是在这种情况下,需要比较对象的值,以下代码将适合您。

playAgain.equals("yes")

答案 4 :(得分:0)

您需要使用String.equals()方法而不是==

来比较字符串

equals方法比较字符串的值,==检查两个对象是否引用同一个对象。