循环中的总计不会加起来

时间:2015-03-16 21:52:09

标签: java loops

所以我决定绕过卡片发生器,这要归功于前一个问题的另一张海报。我从社区得到了很多好主意,我试图不复制粘贴,并尽可能保持工作真实。

这就是为什么有些问题尚未修复,而其他问题已经出现的原因。

那就是说,当我运行这个版本时,总数不能正常加起来,我认为在第二次打击它有点乱。所以我希望多一点鼓励:)

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");
            }

            }
    }


}

}

1 个答案:

答案 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();
    }
}

我做了以下更改:

  • 仅使用一个变量作为ptotal,总结。
  • 删除一个没有任何意义的块{...}
  • 将班级名称大写。 (因为这是一个java会议)
  • 确保在程序中只打开和关闭一台扫描仪。
  • 移动if检查' s '字母,以便可以访问。
  • 修改if-else结构以避免不必要的检查
  • 删除未使用的变量notPlaying
  • 确保接受大写和小写输入。