我无法让Java接受键盘输入字符串
我想知道如何在我的java程序中获得键盘响应以正常工作并继续游戏
我意识到代码有点凌乱,数组可能更适合,但对于这个例子,我只想要关于键盘输入的答案
在达到if和if-else语句之前,游戏运行得足够好。那我真的不知道怎么修
import java.util.Random;
import java.util.Scanner;
class blackjack {
public static void main(String[] args) {
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
String money;
String name;
String hiscore;
String h;
String s;
int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int card3 = 1 + r.nextInt(11);
int card4 = 1 + r.nextInt(11);
int card5 = 1 + r.nextInt(11);
int card6 = 1 + r.nextInt(11);
int card7 = 1 + r.nextInt(11);
int card8 = 1 + r.nextInt(11);
int card9 = 1 + r.nextInt(11);
int card10 = 1 + r.nextInt(11);
int card11 = 1 + r.nextInt(11);
int card12 = 1 + r.nextInt(11);
int card13 = 1 + r.nextInt(11);
int total1 = card2 + card3;
int total2 = total1 + card4;
System.out.println("Welcome to Blackjack ! ");
System.out.println("Score as close to 21 without going over to win ");
System.out.println("What is your name?");
name = keyboard.nextLine();
System.out.println("Hello " + name);
System.out.println("Let's play some BlackJack!");
System.out.println("The dealer shows: \n\t\t" + card1);
System.out.println("Your first card is: \n\t\t " + card2);
System.out.println("Your second card is: \n\t\t" + card3);
System.out.println("giving you a total of " + total1);
System.out.println("Would you like to (H)it or (S)tick?");
if h = keyboard.nextLine();
System.out.println("Your next card is " + card4);
System.out.println("Giving you a new total of: " + total2);
if else s = keyboard.nextLine();
System.out.println("your final score is: "total1);
}
}
# 1 楼答案
那个
if
(和if-else
)语句是中断的。这不是有效的语法。正确的语法应该是:请注意,我说的是正确的语法,但这肯定不会实现您希望它做的事情。看起来你要做的是对照预先确定的字符串“h”和“s”进行检查
将变量存储在某个地方,然后进行比较。请记住,要在Java中比较字符串,需要使用。等于()