两份失踪的退货声明

时间:2015-05-04 03:08:29

标签: java

import java.util.Scanner;

public class BlackJack {

static Scanner stdin = new Scanner(System.in);
static String[] deck = new String[52];
static String dealerCard = "", dealerHiddenCard = "", userCard = "", userCardTwo = "";
static int dealerCounter = 0, userCounter = 0;

public static void main (String[] args) {




    System.out.println("\n***Welcome to the grand Command Line Casino!***" +
         "\n***Today we are only playing single deck Black Jack!***" +
         "\n***But that's what we do everyday! HAHA!***\n");

    //Creates Deck of cards
    String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace" };

    for (int a=0; a < rank.length; a++) {
        for (int b=0; b < suit.length; b++){
            deck[suit.length*a + b] = (rank[a] +" of "+ suit[b]);
        }
    }

    //Assigns two cards to the dealer 
    //Finds the Integer value of each card and adds it to the dealerCounter

    //first dealer card
    System.out.println("----Dealer's Cards:----");
    System.out.println("(Dealer's First Card Placed Face Down)");
    dealerHiddenCard = randomCard(dealerHiddenCard);
    dealerCard = assignValue(dealerHiddenCard);
    dealerCounter = Integer.parseInt(dealerCard);

    //second dealer card
    dealerCard = randomCard(dealerCard);
    System.out.println(dealerCard);
    dealerCard = assignValue(dealerCard);
    dealerCounter += Integer.parseInt(dealerCard);


    //Assigns two cards to the user 
    //Finds the Integer value of each card and adds it to the userCounter

    //first user card
    System.out.println("\n----Your Cards:----");
    userCardTwo = randomCard(userCardTwo);
    System.out.println(userCardTwo);
    userCardTwo = assignValue(userCardTwo);
    userCounter = Integer.parseInt(userCardTwo);

    //second user card
    userCard = randomCard(userCard);
    System.out.println(userCard);
    userCard = assignValue(userCard);
    userCounter += Integer.parseInt(userCard);

/////// HERES ONE /////////
/////// HERES ONE /////////
    userAceHandler(userCounter);
/////// HERES ONE /////////
/////// HERES ONE /////////

        if (userCounter == 21)                {
            System.out.println("Black Jack! You Win!"); }


    hitStay();

    dealersTurn();          
}



public static String randomCard(String draw) {
//Pulls a random card from the deck

    while (true) {
        //Generates a random number between 0 and 52
        int card = (int)(Math.random()*52);
        draw = deck[card];

        //Makes sure the same card isn't drawn twice
        if (draw == "0") {
            continue;}
        else {
            deck[card] = "0";
            return draw;}
    }

}



public static String assignValue(String cardName) {
//Assigns a number value to the card

    if (cardName.contains("2"))    {
        cardName = "2";         }
    if (cardName.contains("3"))    {
        cardName = "3";         }
    if (cardName.contains("4"))    {
        cardName = "4";         }
    if (cardName.contains("5"))    {
        cardName = "5";         }
    if (cardName.contains("6"))    {
        cardName = "6";         }
    if (cardName.contains("7"))    {
        cardName = "7";         }
    if (cardName.contains("8"))    {
        cardName = "8";         }
    if (cardName.contains("9"))    {
        cardName = "9";         }
    if (cardName.contains("10"))   {
        cardName = "10";        }
    if (cardName.contains("Jack")) {
        cardName = "10";        }
    if (cardName.contains("Queen")){
        cardName = "10";        }
    if (cardName.contains("King")) {
        cardName = "10";        }
    if (cardName.contains("Ace"))  {
        cardName = "11";        }

    return cardName;
}



public static void hitStay() {
//Processes a user 'Hit or Stay' selection

    int userInput;

    System.out.println("\nHit or Stay?");
    System.out.println("(1) Hit\n(2) Stay");
    userInput = stdin.nextInt();

    if (userInput == 1){    
    //Draws another card and adds it to the userCounter
    //Decides where to go depending on new userCounter value

        userCard = randomCard(userCard);
        System.out.println("\n"+userCard);
        userCard = assignValue(userCard);
        userCounter += Integer.parseInt(userCard);

/////// HERES ONE /////////
/////// HERES ONE /////////
        userAceHandler(userCounter);
/////// HERES ONE /////////
/////// HERES ONE /////////

        System.out.print("(Current Count = "+userCounter+")");

        if (userCounter == 21)                              {
            System.out.println("\n21! Now it's the dealer's turn");  
                                         }
        if (userCounter < 21)                       {
            hitStay();                   }

        if (userCounter > 21)                       {
            System.out.println("Bust! Maybe next time");
            System.exit(1);                  }
    }   
    if (userInput == 2){                       
        System.out.println("\nAlright, dealer's turn");
        System.out.println("\n(Dealer Flips His Hidden Card Over)");
        System.out.println(dealerHiddenCard);
        System.out.println("Dealer Count = "+dealerCounter+")");    
    }

}



public static void dealersTurn() {
//processes the dealers turn after the user has decided to 'stay'

    /////// HERES ONE /////////
    /////// HERES ONE /////////
    dealerAceHandler(dealerCounter);
    /////// HERES ONE /////////
    /////// HERES ONE /////////

    if (dealerCounter > userCounter)                {
        System.out.println("\nDealer Wins! Maybe next time.");   }
        System.exit(1);

    if (dealerCounter == userCounter && dealerCounter > 17)     {
        System.out.println("\nPush! (tie game)");
        System.exit(1);                      }

    if (dealerCounter > userCounter && dealerCounter < 21 
                    && dealerCounter >= 17)     {
        System.out.println("\nDealer Wins! Maybe next time.");
        System.exit(1);                      }

    if (dealerCounter < userCounter && dealerCounter >= 17)     {
        System.out.println("\nYou Win!");
        System.exit(1);                      }

    if (dealerCounter > 21)                     {
        System.out.println("\nDealer Bust! You Win!");
        System.exit(1);                      }

    if (dealerCounter == 21)                    {
        System.out.println("\nDealer Wins! Maybe next time.");
        System.exit(1);                      }

    if (dealerCounter < userCounter || dealerCounter < 17)      {
        dealerCard = randomCard(dealerCard);
        System.out.println("\n"+dealerCard);
        dealerCard = assignValue(dealerCard);
        dealerCounter += Integer.parseInt(dealerCard);
        System.out.println("(Dealer Count = "+dealerCounter+")");

        dealersTurn();                       }
}



public static int userAceHandler(int userCounter) {
//handles the situation if an Ace is drawn for the user

    if (userCounter >= 22 && userCard == "11")          {
        userCounter = userCounter - 10; 
        return userCounter;                  }
}



public static int dealerAceHandler(int dealerCounter) {
//handles the situation if an Ace is drawn for the dealer

    if (dealerCounter >= 22 && dealerCard == "11")          {
        dealerCounter = dealerCounter - 10;
        return dealerCounter;                    }


}

}

好的!我今天花了大约10个小时制作这个(它是一个基本的二十一点游戏),并且在添加代码底部的最后两个方法之前我已经完美地工作了。他们要在我的代码中处理'Ace'条件......不幸的是,当我尝试编译时,它告诉我,我错过了两者的return语句。我只有5周的编程生涯和一周的“我自己的方法”生涯,所以我没有线索。我以为我做对了。

我会用大量的笔记标记,我称之为方法(每个位置只有3个点)

就像我说的那样,在我添加这些内容之前,它与其他所有内容完美配合。

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:9)

return函数中有userAceHandler个语句,但这还不够。如果if条件不正确怎么办?在这种情况下没有return声明。 Java要求您为非return函数中的每个可能的代码路径提供void语句。

答案 1 :(得分:3)

问题是,在java方法中,你必须总是返回一些东西。只有条件完成后,您的方法才会返回值。你必须返回一些东西,即使它不是。尽量在最后写return 0;或类似的东西。

答案 2 :(得分:1)

正如所讨论的,最好的解决方案是移动return语句

public static int dealerAceHandler(int dealerCounter) {
  if (dealerCounter >= 22 && dealerCard == "11") {
    dealerCounter = dealerCounter - 10;
  }
  return dealerCount;
}

- 原创 - 由于您正在编辑函数的输入,可能只是使方法签名无效而不返回任何内容

注意,您必须使用一个对象来执行此操作,因为Java是通过引用传递的,除了基元

public static void dealerAceHandler(Integer dealerCounter) {
  if (dealerCounter >= 22 && dealerCard == "11") {
    dealerCounter = dealerCounter - 10;
  }
}

否则返回在if语句内,因此如果不满足条件则不返回值