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
  • 确保接受大写和小写输入

您是否尝试过附加一个调试器并单步执行代码以查看其行为偏离您预期的地方?大学鼓励我不要使用任何调试器软件,我应该能够在没有调试器的情况下看到逻辑。显然我不能笑。但我会做这件事作为最后的手段。希望没有必要。我认为这个限制没有意义;)我同意,因为我是一个完全的新手,java在我看来根本不符合逻辑:)我理解他们的建议,但使用背后的逻辑,我怀疑让别人指出你代码中的错误会比他们希望你做得更好的更低!谢谢。我对已经存在的变量有问题,并且没有处理字符串a=ScannerIn.nextLine();我会那样工作的。所以我今天学到了一件新东西移动字母“s”的支票以便其可到达??你能给我解释一下吗?当经销商转向时,notplaying变量将开始使用。但现在是的,这并不有用。我认为使用.equals命令时,大写和小写输入效果良好。但也许我错了。感谢您在这方面的帮助:)如果您有时间,我对您的编辑有一些问题,请张贴在这封邮件上方。您有一个if语句,用于检查您的代码中是否键入了s,以确保键入了h。只是在计算输入后,经销商的代码。因此,它将在读取新输入之前进行计算。你不应该使用其他变量。事实上,问题仍然存在,如果我需要点击多次,那么4张牌的总和是不正确的。它仍然只添加了card1+card2+newCard,因此如果我抽取3+4+(6)+4,我的新总数将是11,而不是17。(6)是第一张新卡,在抽取下一张(4)时不会添加到新总数中。如果你玩几个游戏,打了3次以上,你会发现数学不是很正确,也许我确实改变了一些东西。。。我会再次复制粘贴,看看它是否适合我。
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();
    }
}