Java 循环中的总数相加不起来
所以我决定绕过卡片生成器,多亏了前面一个问题的另一张海报。我从社区中得到了很多好主意,我尽量不复制粘贴,尽量保持作品的真实性 这就是为什么有些问题还没有解决,有些问题已经出现了 也就是说,当我运行这个版本时,总数加起来不正确,我认为在第二次点击时,它有点混乱。因此,我希望得到更多的鼓励:)Java 循环中的总数相加不起来,java,loops,Java,Loops,所以我决定绕过卡片生成器,多亏了前面一个问题的另一张海报。我从社区中得到了很多好主意,我尽量不复制粘贴,尽量保持作品的真实性 这就是为什么有些问题还没有解决,有些问题已经出现了 也就是说,当我运行这个版本时,总数加起来不正确,我认为在第二次点击时,它有点混乱。因此,我希望得到更多的鼓励:) }我对您的代码做了一些修改,现在它可以工作了 import java.util.Random; import java.util.Scanner; class Blackjack { public
}我对您的代码做了一些修改,现在它可以工作了
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();
}
}
我做了以下修改:
- 对ptotal只使用一个变量,这将导致
- 删除一个{…}块,它没有任何意义
- 将类名大写。(因为它是java对流)
- 确保在程序中只打开和关闭一个扫描仪李>
- 移动检查“s”字母的if,使其可访问
- 修改if-else结构以避免不必要的检查
- 删除未使用的变量notPlaying
- 确保接受大写和小写输入
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();
}
}