所以我决定绕过卡片发生器,这要归功于前一个问题的另一张海报。我从社区得到了很多好主意,我试图不复制粘贴,并尽可能保持工作真实。
这就是为什么有些问题尚未修复,而其他问题已经出现的原因。
那就是说,当我运行这个版本时,总数不能正常加起来,我认为在第二次打击它有点乱。所以我希望多一点鼓励:)
import java.util.Random;
import java.util.Scanner;
class blackj
{
public static void main(String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
String name;
boolean playing = true;
boolean notPlaying = true;
int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int dcard1 = 1 + r.nextInt(11);
int dcard2 = 1 + r.nextInt(11);
int ptotal = card1 +card2;
int dtotal = dcard1 +dcard2;
{
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" +dcard1 );
System.out.println("Your first card is: \n\t\t " +card1 );
System.out.println("Your second card is: \n\t\t" +card2 );
System.out.println("Giving you a grand total of: " +ptotal );
while (playing)
{
System.out.println("Would you like to (H)it or (S)tick?");
Scanner hit1 = new Scanner(System.in);
String a = hit1.nextLine();
if(a.equals("h"))
{
int newCard = 1 + r.nextInt(11);
System.out.println("Your next card is " +newCard );
int pcurrent = ptotal +newCard;
System.out.println("Giving you a new total of "+pcurrent);
if ((pcurrent >=22))
{
System.out.println("You Busted! \nSorry! you lose");
playing = false;
}
playing = true;
if(a.equals("s"))
{
System.out.println("You stick at " +pcurrent );
System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
}
}
else
{
System.out.println("Please press H or S");
}
}
}
}
}
答案 0 :(得分:0)
我稍微修改了你的代码,现在它可以工作了。
import java.util.Random;
import java.util.Scanner;
class Blackjack
{
public static void main(String[] args)
{
Random r = new Random();
String name;
Scanner scannerIn = new Scanner(System.in);
boolean playing = true;
int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int dcard1 = 1 + r.nextInt(11);
int dcard2 = 1 + r.nextInt(11);
int ptotal = card1 +card2;
int dtotal = dcard1 +dcard2;
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 = scannerIn.nextLine();
System.out.println("Hello " + name);
System.out.println("Let's play some BlackJack!");
System.out.println("The dealer shows: \n\t\t" +dcard1 );
System.out.println("Your first card is: \n\t\t " +card1 );
System.out.println("Your second card is: \n\t\t" +card2 );
System.out.println("Giving you a grand total of: " +ptotal );
while (playing)
{
System.out.println("Would you like to (H)it or (S)tick?");
String a = scannerIn.nextLine();
if(a.toLowerCase().equals("h"))
{
int newCard = 1 + r.nextInt(11);
System.out.println("Your next card is " +newCard );
ptotal = ptotal +newCard;
System.out.println("Giving you a new total of "+ptotal);
if ((ptotal >=22))
{
System.out.println("You Busted! \nSorry! you lose");
playing = false;
}
}else if(a.toLowerCase().equals("s"))
{
System.out.println("You stick at " +ptotal );
System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
}
else
{
System.out.println("Please press H or S");
}
}
scannerIn.close();
}
}
我做了以下更改: