Here’s the code I have for the core program itself:
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;
}
}
and here’s my runner class:
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();
}
}
}
here’s an example of what should be resulting:
Rock-Paper-Scissors – pick your weapon[R,P,S]:: R
player had R
computer had P
!Computer wins <>!
Do you want to play again? y
and if you put n for playing again, it stops.
main problem is getting compChoice to not say null
Your code in
mainmethod:You initialize the
compChoiceString in thesetPlayers()method, but that method is never called. Then you switch oncompChoice, which is null, thus you get theNullPointerException.This should fix your problem:
But be aware that you are not saving the String returned by
determineWinner(), and then callout.println(game);which will call thedetermineWinner()method again…Also this piece of your code
while(response == response)will always be an endless loop, and i can’t find a
breakstatement in your loop.Also NEVER compare Strings with
==, use equals, google the explanation, there even are hundreds of SO explanations for that. So thisif (playChoice == "R") // <-- WRONGshould become this:
if (playChoice.equals("R")) // <-- rightor
if ("R".equals(playChoice)) // <-- right