[英]Why won't my IF-ELSE-IF statement work correctly?
I am trying to create a simple Blackjack game in Java.我正在尝试用 Java 创建一个简单的二十一点游戏。 I have a menu with possible integer options from 1-4.
我有一个菜单,其中包含 1-4 的可能整数选项。 If the user inputs an integer greater than 4, my IF statement needs to print "invalid integer" and then restart the game.
如果用户输入一个大于4的整数,我的IF语句需要打印“无效整数”,然后重新启动游戏。 If the user inputs a negative value, my ELSE-IF statement needs to do the same thing.
如果用户输入负值,我的 ELSE-IF 语句需要做同样的事情。
However, the statements will only work once, so I can't get "invalid integer" to print if I input values below 0 and values greater than 4 multiple times/back-to-back.但是,这些语句只能工作一次,因此如果我多次/背靠背输入小于 0 的值和大于 4 的值,我将无法打印“无效整数”。
[Redacted] [已编辑]
Any help is appreciated.任何帮助表示赞赏。
If you have defined scnr like this:如果您像这样定义了 scnr:
Scanner scnr = new Scanner(System.in);
I suggest you do a simple switch case statement with a default clause.我建议您使用 default 子句做一个简单的 switch case 语句。
switch (userOpt) {
case 1:
// Option 1 logic...
case 2:
// Option 2 logic...
case 3:
// Option 3 logic...
case 4:
// Option 4 logic...
default:
// Handling invalid input...
}
It's really confusing in the way you've written the code.您编写代码的方式确实令人困惑。 Ie how does case 1 involve get another card?
即案例1如何涉及获得另一张卡? Is all the code written in a main function?
所有代码都写在一个主函数中吗? How about you make a class called blackjack in which you have different methods for getCard(), holdHand() etc. and make a class variable which holds the hand and also gamecount and wins for dealer/player.
您如何创建一个名为 blackjack 的类,其中您有不同的 getCard()、holdHand() 等方法,并创建一个类变量,用于持有手牌、游戏计数和庄家/玩家的胜利。 Would be much easier to understand your code.
理解你的代码会容易得多。 Then you can try you code in your main inside the code.
然后您可以尝试在代码中的 main 中编写代码。
Something like;就像是;
public BlackJack {
**class variables such as hand, player wins etc..**
**methods here**
**main method to try your code**
}
I hope you get what I'm suggesting, I know it might feel like a lot of work to do now when you've written your code but this would help you and anyone who reads your code.我希望你能明白我的建议,我知道当你写完你的代码时,现在可能感觉有很多工作要做,但这会对你和任何阅读你代码的人都有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.