gpt4 book ai didi

java - 两个缺失的返回声明

转载 作者:搜寻专家 更新时间:2023-11-01 01:12:31 25 4
gpt4 key购买 nike

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”条件……不幸的是,当我尝试编译时,它告诉我我缺少两者的返回语句。我的编程生涯只有 5 周,而我的“制作我自己的方法”的职业生涯只有一周,所以我没有任何线索。我认为我做对了。

我会在我调用方法的地方用大量注释进行标记(每个方法总共只有 3 个点)

就像我说的,尽管在我添加这些之前它可以与其他所有东西完美配合。

非常感谢您的帮助!

最佳答案

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

关于java - 两个缺失的返回声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com