So I decided to skirt around the card generator, thanks to another poster on a previous question. I have had a lot of good ideas from the community and I'm trying not to copy paste, and keep the work as genuine as possible.
Which is why some problems aren't fixed yet, and others have popped up.
That said, when I run this version, the totals don't add up properly, I think on the second hit it goes a bit haywire. So I would love a little more encouragement :)
import java.util.Random;
import java.util.Scanner;
class blackj
{
public static void main(String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
String name;
boolean playing = true;
boolean notPlaying = 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 ptotal = card1 +card2;
int dtotal = dcard1 +dcard2;
{
System.out.println("Welcome to Blackjack ! " );
System.out.println("Score as close to 21 without going over to win ");
System.out.println("What is your name?");
name = keyboard.nextLine();
System.out.println("Hello " + name);
System.out.println("Let's play some BlackJack!");
System.out.println("The dealer shows: \n\t\t" +dcard1 );
System.out.println("Your first card is: \n\t\t " +card1 );
System.out.println("Your second card is: \n\t\t" +card2 );
System.out.println("Giving you a grand total of: " +ptotal );
while (playing)
{
System.out.println("Would you like to (H)it or (S)tick?");
Scanner hit1 = new Scanner(System.in);
String a = hit1.nextLine();
if(a.equals("h"))
{
int newCard = 1 + r.nextInt(11);
System.out.println("Your next card is " +newCard );
int pcurrent = ptotal +newCard;
System.out.println("Giving you a new total of "+pcurrent);
if ((pcurrent >=22))
{
System.out.println("You Busted! \nSorry! you lose");
playing = false;
}
playing = true;
if(a.equals("s"))
{
System.out.println("You stick at " +pcurrent );
System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
}
}
else
{
System.out.println("Please press H or S");
}
}
}
}
}
I modified your code a bit and now it works.
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 playing = 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 ptotal = card1 +card2;
int dtotal = dcard1 +dcard2;
System.out.println("Welcome to Blackjack ! " );
System.out.println("Score as close to 21 without going over to win ");
System.out.println("What is your name?");
name = scannerIn.nextLine();
System.out.println("Hello " + name);
System.out.println("Let's play some BlackJack!");
System.out.println("The dealer shows: \n\t\t" +dcard1 );
System.out.println("Your first card is: \n\t\t " +card1 );
System.out.println("Your second card is: \n\t\t" +card2 );
System.out.println("Giving you a grand total of: " +ptotal );
while (playing)
{
System.out.println("Would 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("Your next card is " +newCard );
ptotal = ptotal +newCard;
System.out.println("Giving you a new total of "+ptotal);
if ((ptotal >=22))
{
System.out.println("You Busted! \nSorry! you lose");
playing = false;
}
}else if(a.toLowerCase().equals("s"))
{
System.out.println("You stick at " +ptotal );
System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
}
else
{
System.out.println("Please press H or S");
}
}
scannerIn.close();
}
}
I made the following changes:
Using only one variable for ptotal, which sums up.
Removing one block { ... }, which had no meaning
Capitalize the class name. (Because it's a java convetion)
Ensure that only one scanner is opened and closed within your program.
Moving the if which checks for the 's' letter, so that it is reachable.
Modifiying your if-else structure to avoid unnecessary checks
Removing the unused variable notPlaying
Ensuring that both capitalized and lower-case inputs are accepted.
Related
Firstly, thanks for reading my post and helping me out.
I'm trying to program where I define a random number, the program guesses this random number, and then, depending on whether I say the guess is too high or low, the program guesses again.
When the program guesses a number which is too high, the user is to type "lower" and then the program to only guess numbers which are lower.
Ideally, the program would consider the results of all previous choice. So if guess #1 was too high, this should remain the upper-bound for all iterations afterwards. We don't want only the upper or lower bound changing with every iteration.
What I've tried to do with my code is reset the min and max values used to compute the random number ( ThreadLocalRandom ).
The main issue is that I cannot get the oMin and oMax values to reset. I don't understand how to get around this, sorry if this is a totally silly question.
Thanks again!
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class J4AB_S11_C3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int oMin = 0;
int oMax = 101;
int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
String userReply;
System.out.println("What's the lucky number, kiddo?");
int correctAnswer = scanner.nextInt();
System.out.println("Is " + randomNumber + " the right answer?");
userReply = scanner.next();
while ("Higher".equalsIgnoreCase(userReply)) {
oMin = randomNumber;
}
while ("Lower".equalsIgnoreCase(userReply)) {
oMax = randomNumber;
}
if (userReply.equalsIgnoreCase("Correct")) {
System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
}
}
}
Use do-while for this, if-condition to check higher and lower. And close scanner after the do-while
Scanner scanner = new Scanner(System.in);
int oMin = 0;
int oMax = 101;
String userReply;
int correctAnswer;
do {
int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
System.out.println("What's the lucky number, kiddo?");
correctAnswer = scanner.nextInt();
System.out.println("Is " + randomNumber + " the right answer?");
userReply = scanner.next();
if ("Higher".equalsIgnoreCase(userReply)) {
oMin = randomNumber;
}else if ("Lower".equalsIgnoreCase(userReply)) {
oMax = randomNumber;
}
} while (!userReply.equalsIgnoreCase("Correct"));
System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
scanner.close();
I made a few edits and added a while loop, check it out. I tested it and it seems to be working.
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class J4AB_S11_C3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int oMin = 0;
int oMax = 101;
int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
String userReply;
System.out.println("What's the lucky number, kiddo?");
int correctAnswer = scanner.nextInt();
System.out.println("Is " + randomNumber + " the right answer?");
userReply = scanner.next();
while (!"Correct".equalsIgnoreCase(userReply)){
if ("Higher".equalsIgnoreCase(userReply)) {
oMin = randomNumber;
}
if ("Lower".equalsIgnoreCase(userReply)) {
oMax = randomNumber;
}
randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
System.out.println("Is " + randomNumber + " the right answer?");
userReply = scanner.next();
}
if (userReply.equalsIgnoreCase("Correct")) {
System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
}
}
}
I'm writing a program for an assignment that should give random problems for the user to solve. what I am attempting to make it do is after selecting a problem type and answering one question the program should load the menu up again.
Originally I wrote a method that would be called in the else statement on line 147. The method successfully looped however the assignment specifically asks for a loop to make it happen. I've tried several different ways to change the loops condition statement but I'm not sure where I went wrong? any help would be appreciated.
I want very badly to use a switch statement but I can't as we haven't learned that in class.
// Importing Scanner and Random class for later.
import java.util.Scanner;
import java.util.Random;
class AlgebraTutor {
// Solve for Y method.
public static void solve_for_y() {
// Creation of a random number generator.
Random number_gen = new Random();
// Generates random integers from -100 to 100.
int var_m = number_gen.nextInt(101) - 100;
int var_x = number_gen.nextInt(101) - 100;
int var_b = number_gen.nextInt(101) - 100;
// Print problem out for student to see
System.out.println("Problem: y = " + var_m + "(" + var_x +")" + "+" + var_b);
System.out.println(" m =" + var_m);
System.out.println(" x =" + var_x);
System.out.println(" b =" + var_b);
// This formula will calculate the value of y.
float var_y = (var_m * var_x) + var_b;
// Using the scanners class a scanner object called userInput was created to record students answer. Answer was taken as a string and converted to an integer.
Scanner user_input = new Scanner(System.in);
System.out.println("Please solve for y:");
String user_answer = user_input.nextLine();
int answer = Integer.parseInt(user_answer);
if (answer == var_y){
System.out.println("correct");
}else{
System.out.println("incorrect, The answer is:" + var_y);
}
}
// -------------------------------------------------------------------------
// Solve for M method.
public static void solve_for_m() {
// Creation of a random number generator.
Random number_gen = new Random();
// Generates random integers from -100 to 100.
int var_y = number_gen.nextInt(101) - 100;
int var_x = number_gen.nextInt(101) - 100;
int var_b = number_gen.nextInt(101) - 100;
// Print problem out for student to see.
System.out.println("Problem: " + var_y + " = m (" + var_x +") + " + var_b);
System.out.println(" y =" + var_y);
System.out.println(" x =" + var_x);
System.out.println(" b =" + var_b);
// This formula will calculate the value of m.
float var_m = (var_y - var_b) / var_x;
// Using the scanners class a scanner object called userInput was created to record students answer. Answer was taken as a string and converted to an integer.
Scanner user_input = new Scanner(System.in);
System.out.println("Please solve for m:");
String user_answer = user_input.nextLine();
int answer = Integer.parseInt(user_answer);
if (answer == var_m){
System.out.println("correct");
}else{
System.out.println("incorrect, The answer is:" + var_m);
}
}
// -------------------------------------------------------------------------
// Solve for B method
public static void solve_for_b() {
// Creation of a random number generator.
Random number_gen = new Random();
// Generates random integers from -100 to 100.
int var_y = number_gen.nextInt(101) - 100;
int var_x = number_gen.nextInt(101) - 100;
int var_m = number_gen.nextInt(101) - 100;
// Print problem out for student to see.
System.out.println("Problem: " + var_y + " = " + var_m + " (" + var_x +") " + "+ b");
System.out.println(" y =" + var_y);
System.out.println(" x =" + var_x);
System.out.println(" m =" + var_m);
// This formula will calculate the value of m.
float var_b = var_y / (var_m * var_x);
// Using the scanners class a scanner object called userInput was created to record students answer. Answer was taken as a string and converted to an integer.
Scanner user_input = new Scanner(System.in);
System.out.println("Please solve for b:");
String user_answer = user_input.nextLine();
int answer = Integer.parseInt(user_answer);
if (answer == var_b){
System.out.println("correct");
}else{
System.out.println("incorrect, The answer is:" + var_b);
}
}
// -------------------------------------------------------------------------
public static void main(String[] args) {
do{
System.out.println("Which type of problem would you like to practice?");
System.out.println("1) Solve for y");
System.out.println("2) Solve for m");
System.out.println("3) Solve for b");
System.out.println("4) To quit");
Scanner selection_input = new Scanner(System.in);
String user_selection = selection_input.nextLine();
if ( user_selection.equals("1")){
solve_for_y();
} else if (user_selection.equals("2")){
solve_for_m();
} else if (user_selection.equals("3")){
solve_for_b();
} else if (user_selection.equals("4")){
System.out.println("Quitting Program");
System. exit(0);
} else{
System.out.println("Please choose from the given options");
}
} while(user_selection.equals("1") &&
user_selection.equals("2") &&
user_selection.equals("3") &&
user_selection.equals("4"));
}
}
You must declare the user_inpout variable outside the do...while loop, then you can check its value in the while() expression. Also you should initialize the scanner only once at the beginning of your program.
public static void main(String[] args)
{
Scanner selection_input = new Scanner(System.in);
String user_selection=null;
do
{
System.out.println("Which type of problem would you like to practice?");
System.out.println("1) Solve for y");
System.out.println("2) Solve for m");
System.out.println("3) Solve for b");
System.out.println("4) To quit");
user_selection = selection_input.nextLine();
if (user_selection.equals("1"))
{
solve_for_y();
}
else if (user_selection.equals("2"))
{
solve_for_m();
}
else if (user_selection.equals("3"))
{
solve_for_b();
}
else if (user_selection.equals("4"))
{
System.out.println("Quitting Program");
System.exit(0);
}
else
{
System.out.println("Please choose from the given options");
}
}
while (!user_selection.equals("4"));
}
For the case "4" you have two exists now:
else if (user_selection.equals("4"))
{
System.out.println("Quitting Program");
System.exit(0);
}
and:
while (!user_selection.equals("4"));
Only one of both is needed. So you may either remove the first one or replace the while statement by while(true).
So I was wondering if someone could show me how I can call/reference a variable from one method into another method. For example,
public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}
public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter player 1's name: ");
String p1 = input.nextLine();
System.out.print("Enter player 2's name: ");
String p2 = input.nextLine();
System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}
public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}
I want to use p1 and p2 from playerNames() inside of coinToss() so I can simply announce who goes first, but I just can't figure out how to call the variables.
My question is not really different compared to others, however I was unable to understand the answers others were given. Once I posted this I got the answer from a bunch of kind people :)
I'm assuming you are new to Java, because it seems you aren't familiar with the concept of fields (i.e. you can put variables outside methods).
public class YourClass {
static String p1;
static String p2;
public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}
public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter player 1's name: ");
p1 = input.nextLine();
System.out.print("Enter player 2's name: ");
p2 = input.nextLine();
System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}
public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}
}
what you are searching for is called instance variables, check this out.
https://www.tutorialspoint.com/java/java_variable_types.htm
All I had to do was create the instance/static variables outside! Like this:
static String name1;
static String name2;
It was very easy. Thanks everyone for your help!
So what I have here is a fully functioning roulette game in java, running on eclipse. I want to improve it beyond What it was supposed to be by allowing the user to specify how many players they want to be in the same game, as opposed to the two hardcoded players. currently I put a way to set individual names for player 1 and 2 so me and a friend could play but I feel this would make it more universal. The goal is to take the code I have now and learn how to take a specified number, throw it into a loop to generate player objects (each with their own name) and have the program still function.
I have three classes, the Player class witch holds my constructors and payment methods, and getName() getMoney() methods as well. the Roulette class witch is the main method for the program where this loop to add player objects should be added. and the Wheel class where the randomly generated ball position is held and so forth.
here is the first class:
import java.util.Scanner;
//************************************************************************
// Class Player represents one roulette player.
//************************************************************************
public class Player
{
private int bet, money, betType, number, betNet;
#SuppressWarnings("unused")
private static int houseWins;
private String name;
private Scanner scan = new Scanner(System.in);
//=====================================================================
// The Player constructor sets up name and initial available money.
//=====================================================================
public Player (String playerName, int initialMoney)
{
name = playerName;
money = initialMoney;
} // constructor Player
//=====================================================================
// Returns this player's name.
//=====================================================================
public String getName()
{
return name;
} // method getName
//=====================================================================
// Returns this player's current available money.
//=====================================================================
public int getMoney()
{
return money;
} // method getMoney
//=====================================================================
// returns the net bets for that player
//=====================================================================
public int getNet()
{
return betNet;
}
//=====================================================================
// Prompts the user and reads betting information.
//=====================================================================
#SuppressWarnings("static-access")
public void makeBet(Wheel Wheel)
{
Scanner scan = new Scanner(System.in);
System.out.print("How much do you want to bet " + name + "? : ");
bet = scan.nextInt();
if(bet>=money)
{
System.out.println("im going all in!");
bet= money;
money = money-bet;
}
else
{
money = money-bet;
}
Wheel.betOptions();
System.out.println("Choose your bet type: ");
betType = scan.nextInt();
if(betType == 3){
System.out.println("your'e betting on: a number");
while(true){
System.out.println("Please enter the number you "
+ "wish to bet on from 1 to 36: ");
number = scan.nextInt();
if(number <1 || number > 36)
{
System.out.println("This Number is not in "
+ "the desired range.");
}
else
{
break;
}
}
}
}
// method makeBet
//=====================================================================
// Determines if the player wants to play again.
//=====================================================================
public boolean playAgain(Scanner scan)
{
String answer;
System.out.println (name + " Play again [Y/N]? ");
answer = scan.nextLine();
return (answer.equals("y") || answer.equals("Y"));
}
// method playAgain
public void payment(Wheel Wheel2)
{
int winnings = Wheel2.payoff(bet, betType, number);
money += winnings;
if(winnings > 0)
{
betNet += (winnings-bet);
Roulette.setHouseWins(-(winnings-bet));
}
else
{
betNet -= bet;
Roulette.setHouseWins((bet));
}
// =========================================================
// if player runs out of money.
// =========================================================
if(money < 1)
{
Scanner scan = new Scanner(System.in);
System.out.println(name + ", you're out of money. "
+ "Do you want to bet more? [Y/N] ");
String answer= scan.nextLine();
if(answer.equals("y") || answer.equals("Y"))
{
if (betNet>-200)
{
System.out.println("You bought back in for 100");
money +=100;
}
else {
System.out.println("Sorry buddy, your cutt off from the credit line.");
money = 0;
}
}
}
//setHouseWins(betNet);
System.out.println(" Testing to see how much is "
+ "betNet keeping track of: " + betNet);
}
}
Then here is the Wheel class:
//************************************************************************
// Class Wheel represents a roulette wheel and its operations. Its
// data and methods are static because there is only one wheel.
//************************************************************************
public class Wheel
{
// public name constants -- accessible to others
public final static int BLACK = 0; // even numbers
public final static int RED = 1; // odd numbers
public final static int GREEN = 2; // 00 OR 0
public final static int NUMBER = 3; // number bet
public final static int MIN_NUM = 1; // smallest number to bet
public final static int MAX_NUM = 36; // largest number to bet
// private name constants -- internal use only
private final static int MAX_POSITIONS = 38; // number of positions on wheel
private final static int NUMBER_PAYOFF = 35; // payoff for number bet
private final static int COLOR_PAYOFF = 2; // payoff for color bet
// private variables -- internal use only
private static int ballPosition; // 00, 0, 1 .. 36
private static int color; // GREEN, RED, OR BLACK
//=====================================================================
// Presents welcome message
//=====================================================================
public static void welcomeMessage()
{
System.out.println("Welcome to a simple version of roulette game.");
System.out.println("You can place a bet on black, red, or a number.");
System.out.println("A color bet is paid " + COLOR_PAYOFF + " the bet amount.");
System.out.println("A number bet is paid " + NUMBER_PAYOFF + " the bet amount.");
System.out.println("Have fun and good luck!\n");
}
//=====================================================================
// Presents betting options
//=====================================================================
public static void betOptions()
{
System.out.println("Betting Options:");
System.out.println(" 1. Bet on black");
System.out.println(" 2. Bet on red");
System.out.println(" 3. Bet on a number between " + MIN_NUM +
" and " + MAX_NUM);
System.out.println();
}
//=====================================================================
// method spins the wheel
//=====================================================================
public int spin()
{
ballPosition= (int)(Math.random() * MAX_POSITIONS + 1);;
if(ballPosition < 37)
{
if(ballPosition % 2 == 0)
{
color = BLACK;
}
else
{
color = RED;
}
}
else
{
color = GREEN;
}
System.out.print("the Result is: "+ ballPosition
+ " and color is: ");
if (color == BLACK)
{
System.out.println("BLACK.");
}
else if (color == RED)
{
System.out.println("RED.");
}
else
{
System.out.println("GREEN.");
}
return ballPosition;
}
//=====================================================================
// calculates the payoff
//=====================================================================
public int payoff( int bet, int betType, int number)
{
if (color == GREEN)
{
return 0;
}
else if (betType == 1 && color == BLACK)
{
return bet * COLOR_PAYOFF;
}
else if (betType == 2 && color == RED)
{
return bet * COLOR_PAYOFF;
}
else if (betType == 3 && number == ballPosition)
{
return bet * NUMBER_PAYOFF;
} else return 0;
}
}
and Finally the main method roulette class:
import java.util.*;
//************************************************************************
// Class Roulette contains the main driver for a roulette betting game.
//************************************************************************
public class Roulette
{
private static int houseMoney=0;
//=====================================================================
// Contains the main processing loop for the roulette game.
//=====================================================================
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String name1 = "jane"; String name2 = "don";
Wheel wl1 = new Wheel();
System.out.println("please enter a name for player 1: ");
name1 = scan.nextLine();
System.out.println("please enter a name for player 2: ");
name2 = scan.nextLine();
Player player1 = new Player (name1, 100); // $100 to start for Jane
Player player2 = new Player (name2, 100); // $100 to start for Dave
boolean bl1 = true;
boolean bl2 = true;
Wheel.welcomeMessage();
while (bl1 || bl2)
{
System.out.println ("Money available for " + player1.getName()
+ ": " + player1.getMoney());
if(bl1)player1.makeBet(wl1);
System.out.println ("Money available for " + player2.getName()
+ ": " + player2.getMoney());
if(bl2)player2.makeBet(wl1);
wl1.spin();
if(bl1 == true)
{
//initiate the payment
player1.payment(wl1);
System.out.println ("Money available for " + player1.getName() + ": "
+ player1.getMoney());
// check weather play the game again
if(!player1.playAgain(scan))
{
bl1 = false;
System.out.println(player1.getName()+" total wins/losses: "
+ player1.getNet());
}
}
// check the boolean value
if(bl2 == true)
{
//initiate the payment
player2.payment(wl1);
System.out.println ("Money available for " +
player2.getName() + ": " + player2.getMoney());
// check weather play the game again
if(!player2.playAgain(scan))
{
bl2 = false;
System.out.println(player2.getName()+ " total wins/losses: "
+ player2.getNet());
}
}
}
System.out.println();
System.out.printf("House %s: $%d", houseMoney > 0 ? "Wins" : "Losses", houseMoney);
System.out.println();
System.out.println ("Game over! Thanks for playing.");
}
public static void setHouseWins(int Winnings)
{
houseMoney += Winnings;
}
}
ive tried a few different ways to generate the player objects but I think the real issue is coming up with a boolean object for each respective player that wants to quit to be able to stop playing. please help a guy out! (Ps: feel free to use this program as a basis for your own roulette games as it does function quite well as is.)
I would like to know how to get a keyboard response in my java program to work and continue with the game as normal.
I realize the code is a little messy and an array might suit better, but for this instance I would like only answers about keyboard inputs.
The game works well enough until it reaches the if and if else statement. Then I really don't know how to fix it.
import java.util.Random;
import java.util.Scanner;
class blackjack {
public static void main(String[] args) {
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
String money;
String name;
String hiscore;
String h;
String s;
int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int card3 = 1 + r.nextInt(11);
int card4 = 1 + r.nextInt(11);
int card5 = 1 + r.nextInt(11);
int card6 = 1 + r.nextInt(11);
int card7 = 1 + r.nextInt(11);
int card8 = 1 + r.nextInt(11);
int card9 = 1 + r.nextInt(11);
int card10 = 1 + r.nextInt(11);
int card11 = 1 + r.nextInt(11);
int card12 = 1 + r.nextInt(11);
int card13 = 1 + r.nextInt(11);
int total1 = card2 + card3;
int total2 = total1 + card4;
System.out.println("Welcome to Blackjack ! ");
System.out.println("Score as close to 21 without going over to win ");
System.out.println("What is your name?");
name = keyboard.nextLine();
System.out.println("Hello " + name);
System.out.println("Let's play some BlackJack!");
System.out.println("The dealer shows: \n\t\t" + card1);
System.out.println("Your first card is: \n\t\t " + card2);
System.out.println("Your second card is: \n\t\t" + card3);
System.out.println("giving you a total of " + total1);
System.out.println("Would you like to (H)it or (S)tick?");
if h = keyboard.nextLine();
System.out.println("Your next card is " + card4);
System.out.println("Giving you a new total of: " + total2);
if else s = keyboard.nextLine();
System.out.println("your final score is: "total1);
}
}
That if (and if-else) statement is broken. It's not valid syntax. Correct syntax would be:
if(h = keyboard.nextLine());
Note that I said that was correct syntax, but that will definitely not do what you want it to do. It seems like what you want to do is check against predetermined strings "h" and "s".
Store the variable off somewhere, then do the comparison. Remember that to compare Strings in Java, you need to use .equals().
String response = keyboard.nextLine();
if("h".equals(response)) {
// perform operations here
} else if("s".equals(response)) {
// perform operations here
}