【发布时间】:2016-02-02 18:53:15
【问题描述】:
我对编程比较陌生,所以请耐心等待。我不知道如何将变量 p12c、p13c 等传递给方法findWinner。我经常遇到这个问题,所以如果你能提供一些帮助,我将不胜感激。此外,也欢迎任何有关如何简化任何代码的提示。谢谢。
import java.util.*;
public class StarterPoker {
public static void main(String[] args)
{
do
{
System.out.print("Welcome to Starter Poker!\n\n" +
"To play, two players will be dealt\n"+
"five cards per hand and the hand with\n"+
"the highest cards will win. Then you\n"+
"will be prompted to continue or quit.\n");
dealHand();
findWinner();
}
while(playAgain()=='Y');
}
public static void dealHand()
{
int p12c=0, p13c=0, p14c=0, p15c=0, p16c=0, p17c=0, p18c=0, p19c=0, p1tc=0, p1jc=0, p1qc=0, p1kc=0, p1ac=0;
int p22c=0, p23c=0, p24c=0, p25c=0, p26c=0, p27c=0, p28c=0, p29c=0, p2tc=0, p2jc=0, p2qc=0, p2kc=0, p2ac=0;
int [][] deck = new int[13][4];
int [][] player1hand = new int[5][2];
int [][] player2hand = new int[5][2];
//the p1 = player1, then it has each card value, followed by c for count, for example: p12c = player1 2s count.
String[] suits = {"Spades","Diamonds","Hearts","Clubs"};
String[] cards = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
Random randGen = new Random();
do
{
String hand1 = "";
//dealing hand 1
for(int dex = 0; dex < player1hand.length; dex++)
{
boolean goodCard = false;
while(!goodCard)
{
int suit = randGen.nextInt(4);
int card = randGen.nextInt(13);
if( deck[card][suit] == 0)
{
goodCard = true;
deck[card][suit] = 1;
player1hand[dex][0] = suit;
player1hand[dex][1] = card;
hand1 += "\n "+cards[card]+" of "+suits[suit]+"";
if(card == 0) p12c=p12c+1;
if(card == 1) p13c=p13c+1;
if(card == 2) p14c=p14c+1;
if(card == 3) p15c=p15c+1;
if(card == 4) p16c=p16c+1;
if(card == 5) p17c=p17c+1;
if(card == 6) p18c=p18c+1;
if(card == 7) p19c=p19c+1;
if(card == 8) p1tc=p1tc+1;
if(card == 9) p1jc=p1jc+1;
if(card == 10) p1qc=p1qc+1;
if(card == 11) p1kc=p1kc+1;
if(card == 12) p1ac=p1ac+1;
}
}
}
String hand2 = "";
//dealing hand 1
for(int dex = 0; dex < player2hand.length; dex++)
{
boolean goodCard = false;
while(!goodCard)
{
int suit = randGen.nextInt(4);
int card = randGen.nextInt(13);
if( deck[card][suit] == 0)
{
goodCard = true;
deck[card][suit] = 1;
player2hand[dex][0] = suit;
player2hand[dex][1] = card;
hand2 += "\n "+cards[card]+" of "+suits[suit]+"";
if(card == 0) p22c=p22c+1;
if(card == 1) p23c=p23c+1;
if(card == 2) p24c=p24c+1;
if(card == 3) p25c=p25c+1;
if(card == 4) p26c=p26c+1;
if(card == 5) p27c=p27c+1;
if(card == 6) p28c=p28c+1;
if(card == 7) p29c=p29c+1;
if(card == 8) p2tc=p2tc+1;
if(card == 9) p2jc=p2jc+1;
if(card == 10) p2qc=p2qc+1;
if(card == 11) p2kc=p2kc+1;
if(card == 12) p2ac=p2ac+1;
}
}
}
System.out.print("\nDo you want to deal two hands?\nenter 'Y' to continue or anything else to quit: ");
Scanner input = new Scanner(System.in);
char cont = input.next().charAt(0);
if(cont != 'Y')
{
System.out.println("Program terminated!");
System.exit(0);
}
System.out.printf("Player 1's hand is %s\n", hand1);
System.out.printf("Player 2's hand is %s\n", hand2);
} while(true);
}
public static void findWinner()
{
int p1s = 0, p2s = 0;
int p1p = 0, p2p = 0;
int p1t = 0, p2t = 0;
int p1f = 0, p2f = 0;
if (p12c > 0)
{
if(p12c >1)
{
if(p12c > 2)
{
if(p12c > 3)
{
p1f = 1;
}
else
{
p1t = 1;
}
}
else
{
p1p = p1p+1;
}
}
else
{
p1s = p1s+1;
}
}
if (p13c > 0)
{
if(p13c >1)
{
if(p13c > 2)
{
if(p13c > 3)
{
p1f = 1;
}
else
{
p1t = 1;
}
}
else
{
p1p = p1p+1;
}
}
else
{
p1s = p1s+1;
}
}
System.out.printf("p1s = %d",p1s);
//System.out.printf("Player %d wins ", winner);
//System.out.print("because %d beats %d\n",winCard, loseCard);
}
public static char playAgain(){
System.out.println("Would you like to play again?");
Scanner input = new Scanner(System.in);
char cont = input.next().charAt(0);
return cont;
}
}
【问题讨论】:
-
您的查找获胜者方法不接受任何参数。首先向该方法添加变量。例如
public static void findWinner(int p12c, int p13c)等。然后当您在代码的其他地方调用该方法时,您使用findWinner(p12c, p13c); -
不要在 void 中声明变量。在课程开始时在外面声明它们。或者,将结果用作返回值。您需要有变量来拦截这些返回,但它们不必是全局的。
-
所以如果我在类的开头声明变量,它会说“不能对非静态字段进行静态引用”
-
如果你在一个类的开头声明变量,它们就属于那个类的一个实例。您可以将成员设为静态,否则您需要使用
new创建该类的新实例 -
在 Java 中,它们被称为方法