-1

I have a little bit of programming experience in web development languages such as HTML, CSS, JQuery, JavaScript, PHP, Python etc., but I am no taking an AP Computer Science class at school to learn Java. To see how similar it is to other languages in both syntax and how to go about different problems I decided to make a simple blackjack game that i have made in other languages before.

I have run into and error where I want to check to see what the user has input, and if it is a certain character, output different things such as a help sentence, welcome message, or just simply start or stop the game. For some reason it reads the input and I can log it to the console but it will not run the methods that I have created and called in the if statement inside the while loop. I am going to post the whole script since I have only been programming in Java for 2 weeks or so now and have no idea where my error could be.

import java.util.Scanner;

public class testClass {

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);
    // Creates a scanner to get input from the keyboard

    boolean gameStart = false;
    // starts false, turns true when 'B' is input

    welcome();
    //displays the welcome message

    while (true) {
        // loop to play the game

        String input = kbd.nextLine().toUpperCase();
        // gets the next input from the user

        if (input == "B") {
            // checks the users input

            gameStart = true;
            // 'turns on' the game

            double[] hand = drawCards();
            // makes an array of 2 cards
            System.out.print(input); //REMOVE

        } else if (input == "?") {
            // checks the users input

            help(gameStart);
            // displays the help message depending on if the game has started or not
            System.out.print(input); //REMOVE

        } else if (input == "Q") {
            // checks the users input

            System.out.print(input); // REMOVE
            break;
            // ends the game loop

        } // ends if

    } // ends while


} // end main

public static void welcome() {
    // displays the welcome message

    System.out.printf("Welcome to blackjack! %nEnter 'B' to begin. %nEnter '?' at any time to see help %nEnter 'Q' at any time to quit %n => ");
    // the welcome message

}

public static void help(boolean gameStart) {
    //displays the help message

    if (gameStart) {

        System.out.printf("Enter '?' at any time to see help %nEnter 'Q' at any time to quit %nEnter 'H' to hit %nEnter 'S' to stay %n => ");
        // the help message when the game has already started

    } else {

        System.out.printf("Welcome to blackjack! %nEnter 'B' to begin. %nEnter '?' at any time to see help %nEnter 'Q' at any time to quit %nEnter 'H' to hit %nEnter 'S' to stay %n => ");
        // the help message when the game hasn't started

    }

} //  end help

public static double randCard() {
    // creates a random card

    double card = Math.round(Math.random() * 10);
    // number 1-10

    if (card == 10 || card == 0) {
        // no cards at 10 or 0

        double x = Math.round(Math.random() * 10);
        // variable used to choose J Q K or A when card is 0 or 10

        if (x < 3) {
            // Jack
            card = 11;
        } else if (x < 5) {
            // Queen
            card = 12;
        } else if (x < 7) {
            // King
            card = 13;
        } else {
            // Ace
            card = 10;
        } 

    }

    return card;
    // returns a single card

} // end randCard

public static double[] drawCards() {
    //creates 2 random cards and returns an array

    double card1 = randCard();
    double card2 = randCard();
    // stores the 2 random cards in variables

    double[] cards = {card1, card2};
    // places the 2 random cards in an array

    return cards;
    // returns the array of cards

} // end drawCards

} // end class
AlexCawley
  • 27
  • 1
  • 8

2 Answers2

1

There are list of things you need to correct:

  • As per Java Naming Conventions it's recommended to have class names starting with First letter as caps - Camel Case convention
  • String comparison should be done via equals() and not '==' that compares references.

    That means that, change the occurrences of:

    • input == "B" to input.equals("B")
    • input == "?" to input.equals("?")
    • input == "Q" to input.equals("Q")

Be aware that if the inputis null, it will result in NullPointerException. Better practice would be to use "B".equals(input) in that case. Looks like these are already answered as part of comments as well.

P.S : I can't say that your program will work correct after this change, But correction of these should help you.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

Use equals() method of string instead of ==. Hope that helps.

Rahul Yadav
  • 1,503
  • 8
  • 11