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;
}
}
这是驱动程序....
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.");
}
}
}
我无法理解......我已经尝试重新安排我的代码。我很高兴参加高中的AP课程,并且正在努力学习方法的概念。谢谢你的帮助!
答案 0 :(得分:3)
您尚未创建BlackJackRules实例
此
String card1 = new String();
应该是
BlackJackRules card1 = new BlackJackRules();
然后这将起作用
System.out.println("Your first card is a " + card1.getCard());
由于getCard()
在BlackJackRules
类中,您首先需要创建它的实例以使用其方法
同样在您的方法中,您只想使用此
c = generator.nextInt(13) + 1
答案 1 :(得分:0)
而不是
c = generator.nextInt()+14;
你可能想要。
c = generator.nextInt(13) +1;
写入的代码很可能不会产生所需的结果,即使它编译并运行,因为你将在32位整数的整个范围内生成整数。
答案 2 :(得分:0)
除了之前的答案:
关于第二点,最好将该循环更改为do-while并添加提示,询问用户是否希望绘制另一张卡。