0

这是我为核心程序本身提供的代码:

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();
        
        
        
        
    }
    
}
}

这是应该产生的结果的示例:

Rock-Paper-Scissors - 选择你的武器[R,P,S]:: R

玩家有 R

电脑有P

!电脑赢了<>!

你想再玩一次吗?是的

如果你把 n 再次播放,它就会停止。

主要问题是让 compChoice 不说 null

4

2 回答 2

0

您在main方法中的代码:

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

您在方法中初始化compChoice字符串setPlayers(),但该方法永远不会被调用。然后你打开compChoice,它是空的,这样你就得到了NullPointerException

这应该可以解决您的问题:

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

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

还有你的这段代码

while(response == response)

将永远是一个无限循环,我在你的循环中找不到break语句。

永远不要比较字符串==,使用等于,谷歌解释,甚至有数百种解释。所以这

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

应该变成这样:

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

或者

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

于 2012-11-08T01:09:36.553 回答
0

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

于 2012-11-08T01:14:04.363 回答