Rock Paper Scissor if statements not working - java

When I run this, it displays all System.out.println("") statements in the case. How do I make it choose the correct if statement and nothing else?
And if I could go back to the beginning after it finds an outcome, that would also be helpful.
import java.util.Scanner;
import java.util.Random;
public class TEST {
private static Scanner myScanner;
public static void main(String[] args) {
myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
char reply;
System.out.print("Rock(R) Paper(P) or Scissors(S)? ");
reply = myScanner.findWithinHorizon(".", 0).charAt(0);
randomNumber = myRandom.nextInt(3) + 1;
switch (randomNumber) {
case 1:
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Rock. You lost!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Rock. You tied!");
}
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Rock. You won!");
break;
}
case 2:
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Paper. You lost!");
break;
}
case 3:
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Scissor. You won!");
}
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Scissor. You lost!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Scissor. You tied!");
break;
}
}
}
}

You have two problems:
1) Your if statements don't run because they end in semi-colon. This means the code in {} forms a block and runs either way.
2) Your breaks are in the wrong place. The first only breaks if 'R' (or 'r') is chosen. The later breaks if the case is run.
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Paper. You lost!");
break;
}
vs
if (reply == 'P' || reply == 'p') {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's') {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r') {
System.out.println("Computer: Paper. You lost!");
}
break;
Also, as suggested in the comments, you can use a loop to prompt for input. For example:
while (! done) {
System.out.print("Rock(R) Paper(P) or Scissors(S)? (or Quit(Q) ");
reply = myScanner.findWithinHorizon(".", 0).charAt(0);
// more code here
}
You'll need another case here so you can set done to true and end the loop.

You followed the if's with an extra ;
The syntax for an if statements is:
if(condition){
Expressions;
}
the semicolon makes it:
if(condition)
; //empty line, effectively ignores the if
{
Expressions;
}

Related

Aspects of Rock Paper Scissors program not functioning properly

I am trying to get the "press q to quit" function to work properly, but I'm having some trouble. The "invalid Character" function is also not working properly. How would I go about fixing this? My professor suggested putting the "press q to quit" function in the beginning of my code, but hasn't given me any further instruction.
public static void main(String[] args) {
char userChar = 0;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");
// If player chooses to quit
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
}
// Start of loop
while (userChar != 'q' && userChar != 'Q') {
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
}
// If computer generates 2 (Paper)
else if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
}
// If computer generates 3 (Scissors)
else if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
}
// If player types an invalid character
else {
System.out.println("Invalid input! Please enter a valid character.");
}
userChar = sc.next().charAt(0);
}
}
}
In order to really take care of invalid input you need to do it first thing in the loop. Moreover you can't do it in the else block of your if-else statements because in those if-else you check the randomNumber, not the input, so it doesn't make sense and it will never work. In addition you check the input as what you get from sc.next().charAt(0) but for example if the user insert a string like "R_invalid_something_without_space" you only look at the first letter which is R and you take it as a valid input. I modified your code so that you read a line in each loop iteration and if this line is not q / r / p / s it will consider as invalid input and if its q the program will finish.
public static void main(String[] args) {
char userChar = 0;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");
// Start of loop
while (true) {
// what I changed
// **********************************************************
String line = sc.nextLine().toLowerCase();
if(line.equals("q")){
System.out.println("Player chose to quit. Goodbye!");
break;
}
if(!line.equals("r") && !line.equals("s") && !line.equals("p")) {
System.out.println("Invalid input! Please enter a valid character.");
continue;
}
userChar = line.charAt(0);
// **********************************************************
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
}
// If computer generates 2 (Paper)
else if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
}
// If computer generates 3 (Scissors)
else if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
}
}
}
Well, firstly your condition to quit is outside of scope of while loop which means it will never execute in this given context.
Secondly, condition in your while loop will not be true unless userChar is not q and Q simultaneously, which doesn't make sense.
What I suggest is to refactor your while loop little bit, as such:
while (true) {
//Your logic here
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
break;
}
//Your logic here
}

Incorporating While Loop Into Program

I am creating a program that allows you to play a game similar to paper, scissors rock.
The game works, but I am trying to incorporate a loop into the code that will ask the user if they wish to continue playing.
If yes, it will ask them for another input.
If no, the program will simply state "Thanks for playing"
Here's the code:
import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
public static void main(String[]args)
{
String yourMove;
String compMove;
int compInt;
String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};
Scanner input = new Scanner(System.in);
Random rand = new Random(6);
compInt = rand.nextInt(5)+1;
if (compInt == 1)
compMove = "Monkey";
else if (compInt == 2)
compMove = "Robot";
else if (compInt == 3)
compMove = "Pirate";
else if (compInt == 4)
compMove = "Ninja";
else if (compInt == 5)
compMove = "Zombie";
System.out.println("What do you choose?: ");
yourMove = input.next();
//MONKEY
if(yourMove == "Monkey" || compInt == 1)
System.out.println("Tie");
else if (yourMove== "Monkey" || compInt == 2)
System.out.println("You Win! Monkey Unplugs Robot!");
else if (yourMove=="Monkey" || compInt == 3)
System.out.println("You Lose! Pirate Skewers Monkey!");
else if (yourMove == "Monkey" || compInt==4)
System.out.println("You Win! Monkey fools Ninja!");
else if (yourMove== "Monkey" || compInt==5)
System.out.println("You Lose! Zombie savages monkey!");
//RoBOT
else if(yourMove == "Robot" || compInt == 2)
System.out.println("Tie");
else if (yourMove== "Robot" || compInt == 1)
System.out.println("You Lose! Monkey Unplugs Robot!");
else if (yourMove=="Robot" || compInt == 3)
System.out.println("You Lose! Pirate Drowns Robot!!");
else if (yourMove == "Robot" || compInt==4)
System.out.println("You Win! Robot Chokes Ninja");
else if (yourMove== "Robot" || compInt==5)
System.out.println("You win! Robot Crushes Zombie!");
//PIRATE
else if(yourMove == "Pirate" || compInt == 3)
System.out.println("Tie");
else if (yourMove== "Pirate" || compInt == 1)
System.out.println("You Win! Pirate Skewers Monkey!");
else if (yourMove=="Pirate" || compInt == 2)
System.out.println("You Win! Pirate Drowns Robot!");
else if (yourMove == "Pirate" || compInt==4)
System.out.println("You Lose! Ninja Karate Chops Pirate!");
else if (yourMove== "Pirate" || compInt==5)
System.out.println("You Lose! Zombie Eats Pirate!");
//NINJA
else if(yourMove == "Ninja" || compInt == 4)
System.out.println("Tie");
else if (yourMove== "Ninja" || compInt == 1)
System.out.println("You Lose! Monkey Fools Ninja!");
else if (yourMove=="Ninja" || compInt == 2)
System.out.println("You Lose! Robot Chokes Ninja!");
else if (yourMove == "Ninja" || compInt==3)
System.out.println("You Win! Ninja Karate Chops Pirate!");
else if (yourMove== "Ninja" || compInt==5)
System.out.println("You Win! Ninja Decapitates Zombie!");
//ZOMBIE
else if(yourMove == "Zombie" || compInt == 5)
System.out.println("Tie");
else if (yourMove== "Zombie" || compInt == 1)
System.out.println("You Win! Zombie Savages Monkey!");
else if (yourMove=="Zombie" || compInt == 2)
System.out.println("You Lose! Robot Crushes Zombie!");
else if (yourMove == "Zombie" || compInt==3)
System.out.println("You Win! Zombie Eats Pirate!");
else if (yourMove== "Zombie" || compInt==4)
System.out.println("You Lose! Ninja Decapitates Zombie!");
}
}
You could put the entire logic inside a do-while loop. The condition will be if the character entered is=='y' or 'Y'. The pseudo code should be:
char choice='n';
do
{
< Insert Game logic here >
System.out.println("Do you wanna continue? Enter y or Y for Yes")'
choice = <obtain input using Scanner here>;
}
while(choice=='y'||choice=='Y');
System.out.println("Thanks for Playing");
Well...let's be honest here. Your code actually doesn't work. I mean, yes it runs but it will definitely not carry out the game play properly.
Your random generator for computer game character selection will not work properly. You will need to do it this way:
Random rand = new Random();
compInt = rand.nextInt(5)+1;
Leave the 6 out of the initialization of rand.
The logic within the conditions for all your IF and IF/ELSE statements is set up so that at no time will any User input check past the MONKEY game character and this is because you use the OR (||) operator instead of the AND (&&) operator. The way you have it set up, if the computer chooses ROBOT and a User chooses ZOMBIE then the condition contained within:
else if (yourMove == "Monkey" || compInt == 2) {
System.out.println("You Win! Monkey Unplugs Robot!");
}
will always fall true and display the message:
You Win! Monkey Unplugs Robot!
even though the User entered Zombie. Remember...the condition is basically set to be either or which is exactly what you don't want. You want both conditions to be true so you need to use the AND (&&) operator.
Besides that:
yourMove == "Monkey"
will always give unexpected results and here's why. Use the String.equals() method instead, something like:
yourMove.equals("Monkey")
You declare the characters variable as a string array yet nowhere do you utilize that Array. Why even bother. Just make the characters variable a space delimited String and utilize it to see if the User actually has supplied a character within the game play, for example:
String characters = "Monkey Robot Pirate Ninja Zombie";
yourMove = "";
while (yourMove.equals("")) {
System.out.println("What do you choose? --> ");
yourMove = input.nextLine().toLowerCase();
if (yourMove.equals("quit")) {
System.out.println("Thanks for playing. Bye Bye");
System.exit(0);
}
if (!characters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
System.out.println("You entered an invalid Game Character! Try again...\n");
continue;
}
}
In any case, here is a complete working game:
package okekpejmoropinzee;
import java.util.Random;
import java.util.Scanner;
public class OkekpeJMoropinzee {
static boolean playAgain = true;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (playAgain == true) {
playGame(input);
}
System.out.println("Thanks for playing");
input.close();
}
private static void playGame(Scanner input) {
String yourMove = "", compMove = "";
int compInt = 0;
String gameCharacters = "Monkey Robot Pirate Ninja Zombie";
Random rand = new Random();
compInt = rand.nextInt(5)+1;
if (compInt == 1) { compMove = "Monkey"; }
else if (compInt == 2) { compMove = "Robot"; }
else if (compInt == 3) { compMove = "Pirate"; }
else if (compInt == 4) { compMove = "Ninja"; }
else if (compInt == 5) { compMove = "Zombie"; }
System.out.println(compMove + " --- " + compInt);
yourMove = "";
while (yourMove.equals("")) {
System.out.println("What do you choose? --> ");
yourMove = input.nextLine().toLowerCase();
if (yourMove.equals("quit")) {
System.out.println("Thanks for playing. Bye Bye");
System.exit(0);
}
if (!gameCharacters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
System.out.println("You entered an invalid Game Character! Try again...\n");
continue;
}
}
//MONKEY
if (yourMove.equals("monkey") && compInt == 1) {
System.out.println("Tie");
}
else if (yourMove.equals("monkey") && compInt == 2) {
System.out.println("You Win! Monkey Unplugs Robot!");
}
else if (yourMove.equals("monkey") && compInt == 3) {
System.out.println("You Lose! Pirate Skewers Monkey!");
}
else if (yourMove.equals("monkey") && compInt == 4) {
System.out.println("You Win! Monkey fools Ninja!");
}
else if (yourMove.equals("monkey") && compInt == 5) {
System.out.println("You Lose! Zombie savages monkey!");
}
//ROBOT
else if (yourMove.equals("robot") && compInt == 2) {
System.out.println("Tie");
}
else if (yourMove.equals("robot") && compInt == 1) {
System.out.println("You Lose! Monkey Unplugs Robot!");
}
else if (yourMove.equals("robot") && compInt == 3) {
System.out.println("You Lose! Pirate Drowns Robot!!");
}
else if (yourMove.equals("robot") && compInt == 4) {
System.out.println("You Win! Robot Chokes Ninja");
}
else if (yourMove.equals("robot") && compInt == 5) {
System.out.println("You win! Robot Crushes Zombie!");
}
//PIRATE
else if (yourMove.equals("pirate") && compInt == 3) {
System.out.println("Tie");
}
else if (yourMove.equals("pirate") && compInt == 1) {
System.out.println("You Win! Pirate Skewers Monkey!");
}
else if (yourMove.equals("pirate") && compInt == 2) {
System.out.println("You Win! Pirate Drowns Robot!");
}
else if (yourMove.equals("pirate") && compInt == 4) {
System.out.println("You Lose! Ninja Karate Chops Pirate!");
}
else if (yourMove.equals("pirate") && compInt == 5) {
System.out.println("You Lose! Zombie Eats Pirate!");
}
//NINJA
else if(yourMove.equals("ninja") && compInt == 4) {
System.out.println("Tie");
}
else if (yourMove.equals("ninja") && compInt == 1) {
System.out.println("You Lose! Monkey Fools Ninja!");
}
else if (yourMove.equals("ninja") && compInt == 2) {
System.out.println("You Lose! Robot Chokes Ninja!");
}
else if (yourMove.equals("ninja") && compInt == 3) {
System.out.println("You Win! Ninja Karate Chops Pirate!");
}
else if (yourMove.equals("ninja") && compInt == 5) {
System.out.println("You Win! Ninja Decapitates Zombie!");
}
//ZOMBIE
else if(yourMove.equals("zombie") && compInt == 5) {
System.out.println("Tie");
}
else if (yourMove.equals("zombie") && compInt == 1) {
System.out.println("You Win! Zombie Savages Monkey!");
}
else if (yourMove.equals("zombie") && compInt == 2) {
System.out.println("You Lose! Robot Crushes Zombie!");
}
else if (yourMove.equals("zombie") && compInt==3) {
System.out.println("You Win! Zombie Eats Pirate!");
}
else if (yourMove.equals("zombie") && compInt==4) {
System.out.println("You Lose! Ninja Decapitates Zombie!");
}
//Ask if User wants to play the game again...
String playMore = "";
while (!playMore.equals("y") && !playMore.equals("n")) {
System.out.println("\nDo you want to play another game? (y/n) ");
playMore = input.nextLine().toLowerCase();
}
if (playMore.equals("n")) { playAgain = false; }
}
}
You could do something like this:
import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
public static void main(String[]args)
{
String playAgain;
do{
String yourMove;
String compMove;
int compInt;
String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};
Scanner input = new Scanner(System.in);
Random rand = new Random(6);
compInt = rand.nextInt(5)+1;
if (compInt == 1)
compMove = "Monkey";
else if (compInt == 2)
compMove = "Robot";
else if (compInt == 3)
compMove = "Pirate";
else if (compInt == 4)
compMove = "Ninja";
else if (compInt == 5)
compMove = "Zombie";
System.out.println("What do you choose?: ");
yourMove = input.next();
//MONKEY
if(yourMove == "Monkey" || compInt == 1)
System.out.println("Tie");
else if (yourMove== "Monkey" || compInt == 2)
System.out.println("You Win! Monkey Unplugs Robot!");
else if (yourMove=="Monkey" || compInt == 3)
System.out.println("You Lose! Pirate Skewers Monkey!");
else if (yourMove == "Monkey" || compInt==4)
System.out.println("You Win! Monkey fools Ninja!");
else if (yourMove== "Monkey" || compInt==5)
System.out.println("You Lose! Zombie savages monkey!");
//RoBOT
else if(yourMove == "Robot" || compInt == 2)
System.out.println("Tie");
else if (yourMove== "Robot" || compInt == 1)
System.out.println("You Lose! Monkey Unplugs Robot!");
else if (yourMove=="Robot" || compInt == 3)
System.out.println("You Lose! Pirate Drowns Robot!!");
else if (yourMove == "Robot" || compInt==4)
System.out.println("You Win! Robot Chokes Ninja");
else if (yourMove== "Robot" || compInt==5)
System.out.println("You win! Robot Crushes Zombie!");
//PIRATE
else if(yourMove == "Pirate" || compInt == 3)
System.out.println("Tie");
else if (yourMove== "Pirate" || compInt == 1)
System.out.println("You Win! Pirate Skewers Monkey!");
else if (yourMove=="Pirate" || compInt == 2)
System.out.println("You Win! Pirate Drowns Robot!");
else if (yourMove == "Pirate" || compInt==4)
System.out.println("You Lose! Ninja Karate Chops Pirate!");
else if (yourMove== "Pirate" || compInt==5)
System.out.println("You Lose! Zombie Eats Pirate!");
//NINJA
else if(yourMove == "Ninja" || compInt == 4)
System.out.println("Tie");
else if (yourMove== "Ninja" || compInt == 1)
System.out.println("You Lose! Monkey Fools Ninja!");
else if (yourMove=="Ninja" || compInt == 2)
System.out.println("You Lose! Robot Chokes Ninja!");
else if (yourMove == "Ninja" || compInt==3)
System.out.println("You Win! Ninja Karate Chops Pirate!");
else if (yourMove== "Ninja" || compInt==5)
System.out.println("You Win! Ninja Decapitates Zombie!");
//ZOMBIE
else if(yourMove == "Zombie" || compInt == 5)
System.out.println("Tie");
else if (yourMove== "Zombie" || compInt == 1)
System.out.println("You Win! Zombie Savages Monkey!");
else if (yourMove=="Zombie" || compInt == 2)
System.out.println("You Lose! Robot Crushes Zombie!");
else if (yourMove == "Zombie" || compInt==3)
System.out.println("You Win! Zombie Eats Pirate!");
else if (yourMove== "Zombie" || compInt==4)
System.out.println("You Lose! Ninja Decapitates Zombie!");
System.out.println("Would you like to play again? Type yes to play again.");
playAgain = input.next();
} while(playAgain.equals("yes"));
}
}

For Loops In Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
So I'm creating a simple rock paper scissors and I have made a mistake recently. I'm just beginning to learn and I made a mistake and lost track of where. I'd really really appreciate any pointers of where I made a mistake. When it prints out the prompt, I type in what I want, and it just prints it out again. Thank you!
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class rock_Paper_Scissor {
public static void main (String[] args) {
String playerhand;
boolean x = true;
Scanner input = new Scanner(System.in);
Random num = new Random();
int rand = num.nextInt(2) + 1;
System.out.println("I challenge you to Rock Paper Scissor");
System.out.println("If you want to quit, type exit twice");
System.out.println("Type Rock, Paper, or scissor");
playerhand = input.nextLine();
String hands = playerhand.toLowerCase();
while (x == true) {
if (hands == "rock") {
if (rand == 1) {
System.out.println("Rock vs. Rock: TIE");
} else if (rand == 2) {
System.out.println("Rock vs. Scissor: YOU WIN");
} else if (rand == 3) {
System.out.println("Rock vs. Paper: YOU LOSE");
}
}
else if (hands == "paper") {
if (rand == 1) {
System.out.println("Paper vs. Rock: YOU WIN");
} else if (rand == 2) {
System.out.println("Paper vs. Scissor: YOU LOSE");
} else if (rand == 3) {
System.out.println("Paper vs. Paper: TIE");
}
}
else if (hands == "scissor") {
if (rand == 1) {
System.out.println("Scissor vs. Rock: YOU LOSE");
} else if (rand == 2) {
System.out.println("Scissor vs. Scissor: TIE");
} else if (rand == 3) {
System.out.println("Scissor vs. Paper: YOU WIN");
}
}
else if (hands == "exit") {
System.out.println("Thank you for playing!");
x = false;
}
System.out.println("Please type your hand to play again: ");
hands = input.nextLine();
}
}
}
In your all if condition try to use eqauls() method instead of == like this:
if ("rock".equals(hands)) {
...
else if ("paper".equals(hands)) {
...
else if ("scissor".equals(hands )) {
...
else if ("exit".equals(hands)) {
.equals() is used to compare strings values.
But == compare with references strings.

Java - charAt(), equalsIgnoreCase, if statement testing?

What I want is no matter what the user inputs, if the first letter of their input is either a 'y' or 'n' regardless of case, it will print "game start".
I've tried equalsIgnoreCase() with the "letter" variable but it gives the error: char cannot be dereferenced. Any recommendations will be really appreciated on this! Thanks!
Scanner input = new Scanner(System.in);
System.out.println("Do you want to continue?");
String wesker = input.nextLine();
char letter = wesker.charAt(0);
if(letter == 'y' || letter == 'p'){
System.out.println("Game start");
} else {
System.out.println("Game over");
}
Try use Character#toLowercase():
if (Character.toLowerCase(letter) == 'y' || Character.toLowerCase(letter) == 'n') {
or
if (Character.toUpperCase(letter) == 'Y' || Character.toUpperCase(letter) == 'N') {
or simply
if( letter == 'y' || letter == 'Y' || letter == 'n' || letter == 'N' )
Just check against both cases:
if( letter == 'y' || letter == 'Y' || letter == 'p' || letter == 'P' )
equalsIgnoreCase can be used only by Strings. For your case, if you want to use that method, you can do this:
Scanner input = new Scanner(System.in);
String wesker = input.nextLine();
String letter = wesker.substring(0,1);
if(letter.equalsIgnoreCase("y") || letter.equalsIgnoreCase("n")){
System.out.println("Game start");
} else {
System.out.println("Game over");
}
You could pre-build a set of acceptable characters.
Set<Character> yes = new HashSet<>(Arrays.asList('y','Y','p','P'));
public void test() {
char letter = 'c';
if ( yes.contains(letter)) {
}
}

Why is the line printed twice?

I created a simple Tic Tac Toe game. Everything runs perfect except that at the end of the game, when the program decides the outcome it prints the last line twice. For example if player 1 wins it prints:
Player 1 has won
Player 1 has won
I've tried to write input.nextLine(); after the user enters the integer but with no luck.
Thanks in advance.
Here is my code:
public static void main(String[] args) {
//The board
char board[][] = new char [3][3];
//Players' characters
char player1 = 'X';
char player2 = 'O';
char turn = 0;
int rowNumber;
int columnNumber;
int count = 0;
//Welcome mesage
System.out.println("This is a tic tac toe game! \n\n");
System.out.println("Player1 - X \nPlayer 2 - O \n\n");
Scanner input = new Scanner(System.in);
//We display the board
board(board);
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
count = count % 2;
if (count == 0) {
System.out.println("Player 1 turn! \n");
turn = player1;
count++;
} else if (count == 1) {
System.out.println("Player 2 turn!");
turn = player2;
count++;
}
while (true) {
System.out.println("Enter a row(1-3):");
rowNumber = input.nextInt();
System.out.println("Enter a column(1-3):");
columnNumber = input.nextInt();
if (board[rowNumber - 1][columnNumber - 1] != 'X'
&& board[rowNumber - 1][columnNumber - 1] != 'O') {
board[rowNumber - 1][columnNumber - 1] = turn;
break;
} else {
System.out.println("Place already taken. Enter again!");
}
}
board(board);
checkWin(board, player1);
checkWin(board, player2);
fullBoard(board);
}
System.out.println("Just checking;");
}
public static void board(char board[][]) {
System.out.println(board[0][0] + " | " + board[0][1] + " | "
+ board[0][2]);
System.out.println(board[1][0] + " | " + board[1][1] + " | "
+ board[1][2]);
System.out.println(board[2][0] + " | " + board[2][1] + " | "
+ board[2][2]);
}
public static boolean checkWin(char board[][], char player) {
if (board[0][0] == player && board[0][1] == player && board[0][2] == player ||
board[1][0] == player && board[1][1] == player && board[1][2] == player ||
board[2][0] == player && board[2][1] == player && board[2][2] == player ||
board[0][0] == player && board[1][0] == player && board[2][0] == player ||
board[0][1] == player && board[1][1] == player && board[2][1] == player ||
board[0][2] == player && board[1][2] == player && board[2][2] == player ||
board[0][0] == player && board[1][1] == player && board[2][2] == player ||
board[2][0] == player && board[1][1] == player && board[0][2] == player){
if(player == 'X'){
System.out.println("Player 1 has won!");
}
else if(player == 'O'){
System.out.println("Player 2 has won!");
}
return false;
}else {
return true;
}
}
public static boolean fullBoard(char board[][]) {
if ((board[0][0] == 'X' || board[0][0] == 'O')
&& (board[0][1] == 'X' || board[0][1] == 'O')
&& (board[0][2] == 'X' || board[0][2] == 'O')
&& (board[1][0] == 'X' || board[1][0] == 'O')
&& (board[1][1] == 'X' || board[1][1] == 'O')
&& (board[1][2] == 'X' || board[1][2] == 'O')
&& (board[2][0] == 'X' || board[2][0] == 'O')
&& (board[2][1] == 'X' || board[2][1] == 'O')
&& (board[2][2] == 'X' || board[2][2] == 'O')){
System.out.println("It's a tie!");
return false;
}else {
return true;
}
}
}
Because you are calling method twice, once in loop condition and second inside loop.
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
....
checkWin(board, player1);
checkWin(board, player2);
}
You don't need to call it inside loop, in condition it is sufficient.
Change it to following...
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
....
// Remove method call from here.
}
This is due to your while loop here :
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)){
// stuff
// and then again...
checkWin(board, player1);
checkWin(board, player2);
fullBoard(board);
}

Categories

Resources