简体   繁体   English

不明白为什么我的方法不起作用?

[英]Dont understand why my method isn't working?

public class BlackJackRules
{
    Random generator = new Random();
    int c = 0;
    String card1;

    /* Creates a random number which turns into
     * a string card(card1). 
     */

    public String getCard()
    {
        c = generator.nextInt()+14;
        if(c == 1)
            card1 = "Ace";
        else if(c == 2)
            card1 = "Two";
        else if(c == 3)
            card1 = "Three";
        else if(c == 4)
            card1 = "Four";
        else if(c == 5)
            card1 = "Five";
        else if(c == 6)
            card1 = "Six";
        else if(c == 7)
            card1 = "Seven";
        else if(c == 8)
            card1 = "Eight";
        else if(c == 9)
            card1 = "Nine";
        else if(c == 10)
            card1 = "Ten";
        else if(c == 11)
            card1 = "Jack";
        else if(c == 12)
            card1 = "Queen";
        else if(c == 13)
            card1 = "King";
        return card1;
    }
}

and here is the driver program.... 这是驱动程序。

public class BlackJack
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String userStart = new String();
        String userQuit = new String();
        String card1 = new String();

        System.out.println("Would you like to play some BlackJack?");
        userStart = scan.next();
        if(userStart.equalsIgnoreCase("yes"))
        {
            System.out.println("Here we go...");
            while(userQuit.equalsIgnoreCase("yes"))
            {
                System.out.println("Your first card is a " + card1.getCard());
            }
        }
        else
            System.out.println("Okay.");
        }
    } 
}   

I just can't figure it out... I have tried rearranging my code. 我只是想不通...我尝试重新排列我的代码。 I am fairly new taking an AP course in high-school and am struggling with the concepts of methods. 我是刚上高中的AP课程的新手,并且在方法的概念中挣扎。 Thanks for your help! 谢谢你的帮助!

You haven't created an instance of BlackJackRules 您尚未创建BlackJackRules的实例

This 这个

String card1 = new String(); 

Should be 应该

BlackJackRules card1 = new BlackJackRules();

Then this will work 这样就可以了

System.out.println("Your first card is a " + card1.getCard());

Since getCard() is in the BlackJackRules class, you first need to create an instance of it to use its method 由于getCard()BlackJackRules类中,因此您首先需要创建一个实例来使用其方法

Also in your method, you want to just use this 同样在您的方法中,您只想使用此

c = generator.nextInt(13) + 1

Instead of 代替

c = generator.nextInt()+14;

You probably want. 你可能想要。

c = generator.nextInt(13) +1;

The code as written will most likely not produce the desired result even if it compiles and runs, since you will be generating integers in the full range of a 32-bit integer. 所编写的代码即使编译并运行,也极有可能不会产生预期的结果,因为您将在32位整数的整个范围内生成整数。

In addition to the previous answers: 除了以前的答案:

  • Your BlackJackRules class seems to have an extra set of curly braces. 您的BlackJackRules类似乎还有一组花括号。 Not sure if this is particularly important but it made Eclipse complain anyway. 不知道这是否特别重要,但是无论如何它使Eclipse抱怨。
  • The while loop inside your main method will not run. 您的main方法中的while循环将不会运行。 userQuit hasn't been given a value so your equalsIgnoreCase will fail and the program will end. 没有为userQuit提供值,因此equalsIgnoreCase将失败,程序将结束。

On the second point, it'd probably be best to change that loop to a do-while and add a prompt asking if the user wishes to draw another card. 在第二点上,最好将循环更改为“ do-while”,并添加提示,询问用户是否要画另一张牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM