【发布时间】:2019-04-01 01:17:43
【问题描述】:
我正在制作一个简单的 Black Jack 游戏,我完成并想问我的老师他的想法,因为代码有效,但他说我的返回语句不正确。我知道他们处于无效状态,但我不想返回任何东西,我只是想在某些输赢时返回主方法(即主菜单)。为什么这是不正确的,或者我如何告诉他这是正确的。
我尝试将我的 main 方法放在我的其他方法中,但没有奏效,除了我对 java 相当陌生(和一般编码)并且需要一些帮助。
{
System.out.println("\n READY AND START");
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("what is the dealer called?");
String cname = input.nextLine();
System.out.println("How much money are you willing to bet?");
int wager = input.nextInt();
int playerTotal, computerTotal, playerRunning, computerRunning;
playerTotal = 0;
computerTotal = 0;
Random cards = new Random();
for (int i = 1; i <= 2; i++) {
playerRunning = 2 + cards.nextInt(10);
playerTotal += playerRunning;
System.out.println(name + " your starting card is " + playerRunning);
}
System.out.println(name + " your starting hand total is " + playerTotal);
if (playerTotal == 21) {
System.out.println("i guess the house doesnt always win" + name + " you got a blackjack here is your money $" + (wager * 1.5));
return;
} else if (playerTotal > 21) {
System.out.println(name + " you are bust LOL, thank you for the $" + wager);
return;
}
computerRunning = 2 + cards.nextInt(10);
computerTotal += computerRunning;
System.out.println(cname + " the dealers starting card is " + computerRunning);
computerRunning = 2 + cards.nextInt(10);
computerTotal += computerRunning;
System.out.println(cname + " the dealers is keeping their second card hidden");
if (computerTotal == 21) {
System.out.println(computerTotal + " the house always wins haha, you lost your wager of $" + wager);
return;
} else if (computerTotal > 21) {
System.out.println(computerTotal + "The house has busted with here is your money $" + (wager * 2));
return;
}
input = new Scanner(System.in);
System.out.println(name + " do you want to continue? The dealer thinks you can continue. \n Y or N");
String hit = input.nextLine();
while (hit.equalsIgnoreCase("y")) {
playerRunning = 2 + cards.nextInt(10);
playerTotal += playerRunning;
System.out.println(name + " your new card is " + playerRunning);
System.out.println(name + " your hand total is " + playerTotal);
if (playerTotal == 21) {
System.out.println(" \ni guess the house doesnt always win" + name + " well here is your money and sum $" + (wager * 2));
return;
} else if (playerTotal > 21) {
System.out.println(name + " \nyou are bust LOL, thank you for the wager $" + wager);
return;
}
if (computerTotal > 16) {
System.out.println(cname + " the dealers is okay with their hand");
} else {
computerRunning = 2 + cards.nextInt(10);
computerTotal += computerRunning;
System.out.println(cname + " the dealers draws another card");
if (computerTotal == 21) {
System.out.println(cname + " the dealer got " + computerTotal + " the house always wins haha, you lost your wager of $" + wager);
return;
} else if (computerTotal > 21) {
System.out.println(computerTotal + " The house has busted, you got doulbe your wager $" + (wager * 2));
return;
}
}
System.out.println(name + " do you want a hit? The dealer thinks you can continue. \n Y or N");
hit = input.nextLine();
}
do {
computerRunning = 2 + cards.nextInt(10);
computerTotal += computerRunning;
System.out.println(cname + " the dealers draws another card");
if (computerTotal == 21) {
System.out.println(cname + " the dealer got " + computerTotal + " the house always wins haha, you lost your wager of $" + wager);
return;
} else if (computerTotal > 21) {
System.out.println(computerTotal + " The house has busted, you got doulbe your wager $" + (wager * 2));
return;
}
} while (computerTotal < 16);
if (playerTotal > computerTotal && playerTotal <= 21) {
System.out.println(name + " i guess the house doesnt always win, here is your money $" + (wager * 2));
} else if (computerTotal > playerTotal && computerTotal <= 21) {
System.out.println("the house always wins haha, the dealer had " + computerTotal + " thank you for the money $" + wager);
} else if (computerTotal == playerTotal && computerTotal <= 21 && playerTotal <= 21) {
System.out.println("WE DRAWED??? sigh good game i guess BUT house always wins so thank you for the $" + wager);
} else if (computerTotal > 21 && playerTotal > 21) {
System.out.println("and we both busted so no one wins");
}
}
;
``````
这只是我的游戏方法的一部分,它有我的返回功能,你可以看到它是一个带返回的 void,因为我不想返回任何东西我只想回到主菜单没有真的省。游戏可以运行我不知道为什么会出现问题
【问题讨论】:
-
好吧,也许是因为您首先完成了整个计算机的操作,而没有播放器(我可以看到)。二十一点不是这样玩的。
-
我添加了整个代码,所以你现在可以看到
标签: java computer-science