无法获得 compChoice 不返回空值



这是我对核心程序本身的代码:

import java.util.Scanner;
import static java.lang.System.*;
public class RockPaperScissors
{
private String playChoice;
private String compChoice;
public RockPaperScissors()
{

}
public RockPaperScissors(String player)
{
    playChoice = player;
}
public void setPlayers(String player)
{
    playChoice = player;
    compChoice = "";
    int num;
    num = (int) (Math.random()*3);
    switch(num)
    {
    case 0 : compChoice = "R";break;
    case 1 : compChoice = "P";break;
    case 2 : compChoice = "S";break;
    }
    
    System.out.println(num + " " + compChoice);
    out.print(compChoice);
}
public String determineWinner()
{
    String winner="";
    
    if(playChoice == "R")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Draw Game!";break;
        case "P" : winner = "!Computer wins <<Paper Covers Rock>>!";break;
        case "S" : winner = "!Player wins <<Rock Breaks Scissors>>!";break;
        }
    }
    else if(playChoice == "P")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Player wins <<Paper Covers Rock>>!";break;
        case "P" : winner = "!Draw Game!";break;
        case "S" : winner = "!Computer wins <<Scissors Cuts Paper>>!";break;
        }
    }
    else if(playChoice == "S")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Computer wins <<Rock Breaks Scissors>>!";break;
        case "P" : winner = "!Player wins <<Scissors Cuts Paper>>!";break;
        case "S" : winner = "!Draw Game!";break;
        }
    }
    return winner;
}
public String toString()
{
    String output="";
    
    output = "player had " + playChoice + "n computer had " + compChoice + "n " + determineWinner();
    
    return output;
}
}

这是我的跑步班:

import java.util.Scanner;
import static java.lang.System.*;
public class Lab10d
{
public static void main(String args[])
{
    Scanner keyboard = new Scanner(System.in);
    char response  ;
    
    //add in a do while loop after you get the basics up and running
    
        String player = "";
    
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        
        //read in the player value
        player = keyboard.next();
    
        RockPaperScissors game = new RockPaperScissors(player);
        game.determineWinner();
        out.println(game);
    while(response.equals('y'))
    {
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        player = keyboard.next();
        game.setPlayers(player);
        game.determineWinner();
        out.println(game + "n");
        out.println("would you like to play again? (y/n):: ");
        response=  keyboard.next();
        
        
        
        
    }
    
}
}

这是应该产生的示例:

摇滚纸剪子 - 选择武器[R,P,S] :: R

玩家有r

计算机有p

!计算机获胜&lt;!

您想再次玩吗?y

,如果您将n再次播放,它将停止。

主要问题是获得compchoice,不说null

main方法中的代码:

RockPaperScissors game = new RockPaperScissors(player);
game.determineWinner();

您在setPlayers()方法中初始化compChoice字符串,但是该方法从未调用。然后,您打开compChoice(null),因此获得NullPointerException

这应该解决您的问题:

RockPaperScissors game = new RockPaperScissors(player);
game.setPlayers(player); // <-- insert this line.
game.determineWinner();

但是请注意,您没有保存determineWinner()返回的字符串,然后致电out.println(game);,该字符串将再次调用determineWinner()方法...

也是您的代码

while(response == response)

将永远是无尽的循环,我在您的循环中找不到break语句。

从不将字符串与==进行比较,使用等于Google的解释,甚至有数百个这样的解释。所以这个

if (playChoice == "R") // <-- WRONG

应该成为这个:

if (playChoice.equals("R")) // <-- right

if ("R".equals(playChoice)) // <-- right

我只是快速浏览您的代码,实际上没有测试/运行您的代码。但是我发现了这一点:使用字符串等于比较字符串的方法,不要使用==比较字符串。

相关内容

  • 没有找到相关文章

最新更新