Nim Game Java errors? - java

Hi I am programming a game of Nim,
I ha e most of the code complete but I am having two major problems.
I can't find my errors
I can't seem to get the computer to take its turn
Thanks in advance, here is my code.
import java.util.*;
import java.util.Random;
public class Nim {
public static void main(String[] args) {
int banana = 0;
Random r = new Random();
intro();
banana = r.nextInt(16);
int numStones = (15 + banana);
yorner(numStones);
//kbinput.nextInt();
}
public static void intro () {
System.out.println("Welcome to the game of Nim!");
System.out.println("The Rules of the game are as follows: \n");
System.out.println("1. There are two players in this game; you and the computer.");
System.out.println("2. The game starts with a random number stones ranging from 15 to 30 stones.");
System.out.println("3. Every turn each player takes anywhere between 1 to 3 stones");
System.out.println("4. The player who takes the last stone loses. \n");
System.out.println("Would you like to start the game now? \nPlease enter 'Y' for yes and 'N' for no:");
}
public static void yorner (int numStones){
System.out.println("This game of nim will start with " + numStones + " stones.\n");
Scanner kbinput = new Scanner (System.in);
boolean vInput = false;
do
{
String yorn = kbinput.nextLine();
char input = yorn.charAt(0);
switch(input) {
case 'Y':
case 'y':
vInput = true;
yes(numStones);
break;
case 'N':
case 'n':
System.out.println("Thank you for your time.");
vInput = true;
break;
default:
System.out.println("Please only enter 'Y' for yes and 'N' for no, other entries will not be tolerated.");
}
}
while((vInput == false));
}
public static void yes (int numStones){
System.out.println("You selected 'Yes', thank you for choosing to play the game of Nim.\n");
System.out.println("It is your turn first.");
System.out.println("How many stones would you like to take? \n");
System.out.println("Enter a number from 1 to 3");
player(numStones);
}
public static int player(int numStones){
Scanner kbinput = new Scanner (System.in);
int numTake = kbinput.nextInt();
int numStone = 0;
boolean apple = false;
loop: while ( apple == false){
switch(numTake){
case 1:
apple = true;
numStone = numStones - numTake;
System.out.println("There are " + numStone + " stones left");
break;
case 2:
apple = true;
numStone = numStones - numTake;
System.out.println("There are " + numStone + " stones left");
break;
case 3:
apple = true;
numStone = numStones - numTake;
System.out.println("There are " + numStone + " stones left");
break;
default:
System.out.println("You can only takes anywhere between 1 and 3 stones from the pile");
}
}
return numStone;
}
public static boolean compWin (int numStone){
return false;
}
public static void computerTurn(int numStone1, int numStone) {
Random rn = null;
int compTake = rn.nextInt(3);
switch(compTake){
case 1:
System.out.println("Computer takes 1 stone.");
numStone1 = numStone - compTake;
System.out.println("There are " + numStone + " stones left");
break;
case 2:
System.out.println("Computer takes 2 stones");
numStone1 = numStone - compTake;
System.out.println("There are " + numStone + " stones left");
break;
case 3:
System.out.println("Computer takes 3 stones");
numStone1 = numStone - compTake;
System.out.println("There are " + numStone + " stones left");
break;
}
}
}
The errors that I get are these
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert ";" to complete LocalVariableDeclarationStatement
Syntax error, insert ";" to complete Statement

I cannot reproduce your compilation error.
However, you do not have a game loop (that is, a loop that keeps the game going while there are still valid moves) and you never invoke the computerTurn

Related

Rock Paper Scissors programme [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have been frustrated with this code. The goal is to create a rock paper scissors game, best of 3. I have tried to make a "while" loop but I've got it all wrong and ended up scrapping it back to what I started with, a non-looping code.
If anyone can enlighten me how to make it loop until the computer or player gets a score of 3 wins, I would be so grateful!! Thank you so much
Sorry I should clarify, it needs to loop until the user or computer has 3 wins, so they rebattle until the user or computer reaches 3 wins
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main(String[] args)
{
Scanner key;
Random randGen;
char userChoice, computerChoice;
int winner, compInt;
int computerScore;
int userScore;
int scoreCounter;
/***** INITIALIZATION SECTION *****/
key = new Scanner(System.in);
randGen = new Random();
userChoice = ' ';
computerChoice = ' ';
//winner = -1;
compInt = -1;
computerScore = 0;
userScore = 0;
scoreCounter = 0;
System.out.println("What is your name?");
String name = key.nextLine();
System.out.println(name + " play rock, paper, or scissors with me!!");
/***** INPUT SECTION *****/
System.out.println("Please enter R for rock, P for paper, or S for scissors:");
userChoice = key.next().toUpperCase().charAt(0);
//key.close();
/***** PROCESSING SECTION *****/
compInt=randGen.nextInt(3);
if (compInt == 0)
{
computerChoice = 'R';
}
else if (compInt == 1)
{
computerChoice = 'P';
}
else if (compInt == 2)
{
computerChoice = 'S';
}
winner = RockPaperScissors.decideWinner(userChoice,computerChoice);
/***** OUTPUT SECTION *****/
System.out.printf("Player: %c%nComputer: %c%n", userChoice, computerChoice);
switch (winner)
{
case 0:
System.out.println("It's a tie!");
System.out.println(name + ": " + userScore + ", Computer:" + computerScore );
break;
case 1:
System.out.println("You won!");
userScore++;
System.out.println(name + ": " + userScore + ", Computer:" + computerScore );
break;
case 2:
System.out.println("Computer won!");
computerScore++;
System.out.println(name + ": " + userScore + ", Computer:" + computerScore );
break;
default:
System.out.println("Invalid choice, try again.");
break;
}
//System.out.println("Thanks for playing!);
}
/***** STATIC METHODS *****/
/**Description: given two characters (R,P, or S) determines winner using rock
* paper rules. Assume input is valid and error checking is done in main
* programme. **/
public static int decideWinner(char p1,char p2)
{
String combo;
combo = String.format("%c%c", p1, p2);
switch(combo)
{
case "RS":
case "PR":
case "SP":
return 1;
case "RP":
case "PS":
case "SR":
return 2;
case "RR":
case "PP":
case "SS":
return 0;
}
return -1;
}
}
/***************************************************
while(userScore == 3)
{
System.out.println("You won the game" +name+ "! Congratulations");
scoreCounter++;
}
while(computerScore == 3)
{
System.out.println("Sorry " +name+ ", Computer won...");
scoreCounter++;
}
**************************************************/
Try splitting your code in methods that each have their own task, that way u can more easily lay out and control the behaviour of your program. E.g. in the case of user input, you can recursively call the method in case of invalid input.
For the while loop, I simply added a counter that increases after every game, until 3.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
private static int totalGames = 0;
public static void main(String[] args)
{
Scanner key;
Random randGen;
char userChoice, computerChoice;
int winner;
int computerScore;
int userScore;
/***** INITIALIZATION SECTION *****/
key = new Scanner(System.in);
randGen = new Random();
computerChoice = ' ';
//winner = -1;
computerScore = 0;
userScore = 0;
System.out.println("What is your name?");
String name = key.nextLine();
System.out.println(name + " play rock, paper, or scissors with me!!");
while(totalGames < 3) {
userChoice = getUserChoice(key, false);
/* PROCESSING SECTION */
computerChoice = getComputerChoice(computerChoice, randGen.nextInt(3));
winner = RockPaperScissors.decideWinner(userChoice, computerChoice);
totalGames++;
/* OUTPUT SECTION */
System.out.printf("Player: %c%nComputer: %c%n", userChoice, computerChoice);
switch (winner) {
case 0:
System.out.println("It's a tie!");
System.out.println(name + ": " + userScore + ", Computer:" + computerScore);
break;
case 1:
System.out.println("You won!");
userScore++;
System.out.println(name + ": " + userScore + ", Computer:" + computerScore);
break;
case 2:
System.out.println("Computer won!");
computerScore++;
System.out.println(name + ": " + userScore + ", Computer:" + computerScore);
break;
default:
System.out.println("Invalid choice, try again.");
break;
}
}
//System.out.println("Thanks for playing!);
}
private static char getComputerChoice(char computerChoice, int compInt) {
if (compInt == 0)
{
computerChoice = 'R';
}
else if (compInt == 1)
{
computerChoice = 'P';
}
else if (compInt == 2)
{
computerChoice = 'S';
}
return computerChoice;
}
private static char getUserChoice(Scanner key, boolean retry) {
char userChoice;
/* INPUT SECTION */
if(retry)
System.out.println("You entered an invalid input, please try again...");
System.out.println("Please enter R for rock, P for paper, or S for scissors:");
try {
userChoice = key.next().toUpperCase().charAt(0);
if(userChoice != 'R' && userChoice != 'P' && userChoice != 'S')
return getUserChoice(key, true);
} catch (Exception e){
return getUserChoice(key, true);
}
return userChoice;
}
/***** STATIC METHODS *****/
/**Description: given two characters (R,P, or S) determines winner using rock
* paper rules. Assume input is valid and error checking is done in main
* programme. **/
public static int decideWinner(char p1,char p2)
{
String combo;
combo = String.format("%c%c", p1, p2);
switch(combo)
{
case "RS":
case "PR":
case "SP":
return 1;
case "RP":
case "PS":
case "SR":
return 2;
case "RR":
case "PP":
case "SS":
return 0;
}
return -1;
}
}
/***************************************************
while(userScore == 3)
{
System.out.println("You won the game" +name+ "! Congratulations");
scoreCounter++;
}
while(computerScore == 3)
{
System.out.println("Sorry " +name+ ", Computer won...");
scoreCounter++;
}
**************************************************/

Looping Java switch statement

My system is a banking system and I want my program to loop after the user has interacted with one of the processes (withdraw, view balance etc).
After user get result I want to give a chance to continue with another process:
package smartcode.atm.h.w;
import java.util.Scanner;
public class SmartCodeATMHW {
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
return;
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}
This here:
for (int i = 0; i < 3; i++) {
Around that other loop:
} while (end == false)
is simply bogus.
You want one loop that goes around your "menu"; and that loop ends when the user wants to end.
End of story.
I can see from the code you have already handled the requirement using end==false.
You need to remove the return; statement used after the switch case.
Also, for case 1 use break; instead of return;
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
break;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
You can also take input from the User whether he wants to continue or not and depending on the response, display the options again.
just put all cases into a infinite loop and when user select case 4 then system.exit(0); will terminate program otherwise it will loop.
import java.util.Scanner;
public class Test {
public static void main(String [] args){
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
while(true){
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
System.exit(0);
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}

How to fix my RockPaperScissors game written in Java?

I'm trying to write a RockPaperScissors game for my Java class but I'm having trouble with my code. When it runs, sometimes it outputs the wrong thing and sometimes it is correct.
For example when the user enters P the computer is either supposed to answer with Tie, My Point, or Your Point, and then under it will say what each of us played. But often it will say some thing like "Your Point!" "R beats R".
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int tie = 0;
int win = 0;
int loss = 0;
String playerChoice;
System.out.println(RockPaperScissors.getComputerChoice());
while (true)
{
System.out.println();
System.out.print("(R)ock, (P)aper, (S)cissors, or quit: ");
playerChoice = input.nextLine();
if (playerChoice.equalsIgnoreCase("quit")) break;
else
//switch statement
if (playerChoice.equalsIgnoreCase(
RockPaperScissors.getComputerChoice()))
{
System.out.println("Tie!");
tie++;
}
else if ((playerChoice.equalsIgnoreCase("R") &&
RockPaperScissors.getComputerChoice().equals("S")) ||
(playerChoice.equalsIgnoreCase("P") &&
RockPaperScissors.getComputerChoice().equals("R")) ||
(playerChoice.equalsIgnoreCase("S") &&
RockPaperScissors.getComputerChoice().equals("P")))
{
System.out.println("Your Point!");
System.out.println(playerChoice + " beats "
+ RockPaperScissors.getComputerChoice());
win++;
}
else if ((playerChoice.equalsIgnoreCase("R") &&
RockPaperScissors.getComputerChoice().equals("P")) ||
(playerChoice.equalsIgnoreCase("P") &&
RockPaperScissors.getComputerChoice().equals("S")) ||
(playerChoice.equalsIgnoreCase("S") &&
RockPaperScissors.getComputerChoice().equals("")))
{
System.out.println("My Point!");
System.out.println(RockPaperScissors.getComputerChoice()
+ " beats " + playerChoice);
loss++;
}
else
{
System.out.println("Invalid Input!");
}
}
System.out.println();
System.out.println("You won " + win + " times.");
System.out.println("You lost " + loss + " times.");
System.out.println("We tied " + tie + " times.");
}
public static String getComputerChoice ()
{
int compChoiceInt;
String compChoice;
compChoiceInt = (int) (Math.random() * 3);
switch (compChoiceInt)
{
case 0:
compChoice = "R";
break;
case 1:
compChoice = "P";
break;
case 2:
compChoice = "S";
break;
default:
compChoice = "Invalid Input";
System.out.println("Invalid Input.");
break;
}
return compChoice;
}
You're calling getComputerChoice() many times in your code that decides if the user wins, loses, or ties. That can result in several different possible outcomes to each round because each one of those method calls results in a new choice being randomly generated. Instead of calling that method several times, declare a variable and call it just once, before you compare it to the player's choice.

How to replay basic High Low game?

I am almost finished with my second Java assignment for class, but I am stuck on how to take the user's last choice and either play again or quit. Any ideas?
import java.util.Random;
import java.util.Scanner;
public class HighLow {
public static void main(String[]args) {
Random randomNumber = new Random();
int answer = randomNumber.nextInt(100) + 1;
int numberOfTries = 0;
Scanner userInput = new Scanner(System.in);
String userGuess = null;
boolean gameWin = false;
do {
while (!gameWin) {
System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
userGuess = userInput.nextLine();
numberOfTries++;
if (userGuess.equals("quit"))
break;
if (Integer.parseInt(userGuess) == answer) {
gameWin = true;
} else if (Integer.parseInt(userGuess) < answer) {
System.out.println("Your guess is too low");
} else if (Integer.parseInt(userGuess) > answer) {
System.out.println("Your guess is too high");
}
}
if (userGuess.equals("quit")) {
System.out.println("You choose to quit! Thanks for playing");
System.out.println("The number was " + answer);
System.exit(0);
}
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
}
while (userGuess.equals("yes"));
userInput.nextLine();
}
}
}
The problem is because you have userInput.nextLine() prompt for the user to play again or not outside of the game loop and it isn't assigned to any value. I am referring to this section...
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
}
while (userGuess.equals("yes"));
userInput.nextLine(); <---
}
To fix this problem you simply need to assign the final input call to userGuess and place it back into the game loop like so.
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
userGuess = userInput.nextLine();
}
while (userGuess.equals("yes"));
}
Doing this will assign the play again value you prompt the user for into the variable that your testing with the do-whileloop. In your original, you had the condition testing for a value that userGuess wasn't going to be storing and instead prompting the user for that option AFTER the loop would have been exited instead of inside where it needs to be.
To reset the values for the next game, you can simply move the variables' initialization into the game loop at the top like so...
import java.util.Random;
import java.util.Scanner;
public class HighLow {
public static void main(String[]args) {
Random randomNumber = new Random();
int numberOfTries, answer;
Scanner userInput = new Scanner(System.in);
String userGuess;
boolean gameWin;
do {
answer = randomNumber.nextInt(100) + 1;
gameWin = false;
userGuess = null;
numberOfTries = 0;
while (!gameWin) {
System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
userGuess = userInput.nextLine();
numberOfTries++;
if (userGuess.equals("quit"))
break;
if (Integer.parseInt(userGuess) == answer) {
gameWin = true;
} else if (Integer.parseInt(userGuess) < answer) {
System.out.println("Your guess is too low");
} else if (Integer.parseInt(userGuess) > answer) {
System.out.println("Your guess is too high");
}
}
if (userGuess.equals("quit")) {
System.out.println("You choose to quit! Thanks for playing");
System.out.println("The number was " + answer);
System.exit(0);
}
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
userGuess = userInput.nextLine();
}
while (userGuess.equals("yes"));
}
}
}
this way every time the game loops through, all the values will be reset at the beginning of the new game loop.
Also you should close the Random object and Scanner object at the end of the entire program using close() on each instance.
You need to initialize most of your variables and call userInput.nextLine(); inside your outer loop. Untested suggestion:
import java.util.Random;
import java.util.Scanner;
public class HighLow {
public static void main(String[]args) {
String userGuess = null;
do {
Random randomNumber = new Random();
int answer = randomNumber.nextInt(100) + 1;
int numberOfTries = 0;
Scanner userInput = new Scanner(System.in);
boolean gameWin = false;
while (!gameWin) {
System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
userGuess = userInput.nextLine();
numberOfTries++;
if (userGuess.equals("quit"))
break;
if (Integer.parseInt(userGuess) == answer) {
gameWin = true;
} else if (Integer.parseInt(userGuess) < answer) {
System.out.println("Your guess is too low");
} else if (Integer.parseInt(userGuess) > answer) {
System.out.println("Your guess is too high");
}
}
if (userGuess.equals("quit")) {
System.out.println("You choose to quit! Thanks for playing");
System.out.println("The number was " + answer);
System.exit(0);
}
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
}
userInput.nextLine();
while (userGuess.equals("yes"));
}
}
}

while loop if statements

I've almost got this thing working, I just can't manage to get the:
if((pscore <= card1 +card2)) statement to loop until the player either sticks or busts.
from what I can see, it should work... but I'm missing a detail, and I can't figure out what.
import java.util.Random;
import java.util.Scanner;
class Blackjack
{
public static void main(String[] args)
{
Random r = new Random();
String name;
Scanner scannerIn = new Scanner(System.in);
boolean yourTurn = true;
boolean dealersTurn =true;
int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int dcard1 = 1 + r.nextInt(11);
int dcard2 = 1 + r.nextInt(11);
int pscore = card1 +card2;
int dscore = dcard1 +dcard2;
System.out.println("Welcome to Blackjack ! " );
System.out.println("\nScore as close to 21 without going over to win ");
System.out.println("\nWhat is your name?");
name = scannerIn.nextLine();
System.out.println("\nHello " + name);
System.out.println("\nLet's play some BlackJack!");
System.out.println("\nThe dealer shows:\t" +dcard1 );
System.out.println("\n\nYour first card is:\t " +card1 );
System.out.println("\nYour second card is:\t" +card2 );
System.out.println("\nGiving you a grand total of: " +pscore );
while (yourTurn)
{
if ((pscore <= +card1 +card2))
System.out.println("\nWould you like to (H)it or (S)tick?");
String a = scannerIn.nextLine();
if(a.toLowerCase().equals("h"))
{
int newCard = 1 + r.nextInt(11);
System.out.println("\nYour next card is " +newCard );
pscore = pscore +newCard;
System.out.println("\nGiving you a new total of "+pscore);
if ((pscore >=22))
{
System.out.println("\nYou Busted! \nSorry! you lose");
yourTurn = false;break;
}
}
else if(a.toLowerCase().equals("s"))
{
yourTurn = false;
System.out.println("\nYou stick at " +pscore );
System.out.println("\nNow it's the dealers turn\n Dealer must draw until 17");
}
else
{
System.out.println("\nPlease press H or S");
}
while (dealersTurn)
{
dealersTurn = true;
{ if ((dscore <= dcard1+dcard2))
System.out.println("\nThe dealers cards were:\n " +dcard1);
System.out.println("\nand\n" +dcard2);
System.out.println("\nGiving the Dealer a grand total of: " +dscore );
}
{
int newCard1 = 1 + r.nextInt(11);
if ((dscore<=17))
System.out.println("\nThe dealer draws a: " +newCard1 );
dscore = dscore +newCard1;
System.out.println("\nGiving the dealer a grand total of: "+dscore);
}
if ((dscore >=22))
{
System.out.println("\nDealer Bust!");
System.out.println("\nThe House loses");
System.out.println("\nYou Win");
dealersTurn = false;break;
}
else if ((dscore >=pscore))
{
System.out.println("\nDealer has " +dscore);
System.out.println("\nThe dealer beat you!");
System.out.println("\nThe House Wins!");
dealersTurn = false;break;
}
}
}
scannerIn.close();
}
}
Also, I have a bunch of people to thank, helping me get this far. If there is a +1 button for people I can't find it.
Thanks for the help
Vincent.
Your while(yourTurn) loop does not close its bracket until after the while(dealerTurn) loop. This is causing the dealers turn to be a part of the yourTurn loop. Add a closing bracket above the dealersTurn while loop as follows:
}
while (dealersTurn)
{
Then remove the old closing bracket from the bottom above "scannerIn.close()"
But also, what is the purpose of your logic
if ((pscore <= +card1 +card2))
Your score is = to card1 + card2 and then if you draw another card your score will be greater than those 2 cards since you have 3 cards now. That is why it is also not entering your if statement.

Categories