我正在尝试编写一张3张牌扑克游戏并且已经遇到了#34;表达式的类型必须是数组类型,但它解析为int"我感到困惑的错误。这个错误似乎不像我做我的计数器的方式,但我在编码时很新,并且不明白错误在说什么。如果我不得不猜测我会说我的方法,但是当涉及到java时我很无能为力。下面有3个点,我收到错误。我已经包含了给我带来麻烦的部分以及它下面的整套代码。任何帮助表示赞赏。
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//Setting variables
int card1;
int card2;
int card3;
int faceValue1;
int faceValue2;
int faceValue3;
int suit1;
int suit2;
int suit3;
int suit4;
Random g = new Random();
card1 = g.nextInt(51)+1;
card2 = g.nextInt(51)+1;
card3 = g.nextInt(51)+1;
suit1 = g.nextInt(3)+1;
suit2 = g.nextInt(3)+1;
suit3 = g.nextInt(3)+1;
suit4 = g.nextInt(3)+1;
}
public static String FaceValue(String main){
//Making the players hand
int card1;
int card2;
int card3;
int faceValue1;
int faceValue2;
int faceValue3;
int suit;
int suit1;
int suit2;
int suit3;
int suit4;
faceValue1 = card1 /4 + 2;
faceValue2 = card2 / 4 + 2;
faceValue3 = card3 /4 + 2;
System.out.println(faceValue1);
System.out.println(faceValue2);
System.out.println(faceValue3); }
//setting up a method to convert the cards and suit to a string
public static String convert(String main){
int card1;
int card2;
int card3;
int faceValue1;
int faceValue2;
int faceValue3;
int suit;
int suit1;
int suit2;
int suit3;
int suit4;
suit = card1 % 4;
//Converting card and suit 1
if (faceValue1 == 1)
{
System.out.print("Ace of ");
}
if (faceValue1 == 2)
{
System.out.print("Two of ");
}
if (faceValue1 == 3)
{
System.out.print("Three of ");
}
if (faceValue1 == 4)
{
System.out.print("Four of ");
}
if (faceValue1 == 5)
{
System.out.print("Five of ");
}
if (faceValue1 == 6)
{
System.out.print("Six of ");
}
if (faceValue1 == 7)
{
System.out.print("Seven of ");
}
if (faceValue1 == 8)
{
System.out.print("Eight of ");
}
if (faceValue1 == 9)
{
System.out.print("Nine of ");
}
if (faceValue1 == 10)
{
System.out.print("Ten of ");
}
if (faceValue1 == 11)
{
System.out.print("Jack of ");
}
if (faceValue1 == 12)
{
System.out.print("Queen of ");
}
if (faceValue1 == 13)
{
System.out.print("King of ");
}
if (suit1 == 1)
{
System.out.print("Spades");
System.out.println();
}
if (suit1 == 2)
{
System.out.print("Hearts");
System.out.println();
}
if (suit1 == 3)
{
System.out.print("Diamonds");
System.out.println();
}
if (suit1 == 4)
{
System.out.print("Clubs");
System.out.println();
}
//Converting card and suit 2
if (faceValue2 == 1)
{
System.out.print("Ace of ");
}
if (faceValue2 == 2)
{
System.out.print("Two of ");
}
if (faceValue2 == 3)
{
System.out.print("Three of ");
}
if (faceValue2 == 4)
{
System.out.print("Four of ");
}
if (faceValue2 == 5)
{
System.out.print("Five of ");
}
if (faceValue2 == 6)
{
System.out.print("Six of ");
}
if (faceValue2 == 7)
{
System.out.print("Seven of ");
}
if (faceValue2 == 8)
{
System.out.print("Eight of ");
}
if (faceValue2 == 9)
{
System.out.print("Nine of ");
}
if (faceValue2 == 10)
{
System.out.print("Ten of ");
}
if (faceValue2 == 11)
{
System.out.print("Jack of ");
}
if (faceValue2 == 12)
{
System.out.print("Queen of ");
}
if (faceValue2 == 13)
{
System.out.print("King of ");
}
if (suit2 == 1)
{
System.out.print("Spades");
System.out.println();
}
if (suit2 == 2)
{
System.out.print("Hearts");
System.out.println();
}
if (suit2 == 3)
{
System.out.print("Diamonds");
System.out.println();
}
if (suit2 == 4)
{
System.out.print("Clubs");
System.out.println();
}
// Converting card and suit 3
if (faceValue3 == 1)
{
System.out.print("Ace of ");
}
if (faceValue3 == 2)
{
System.out.print("Two of ");
}
if (faceValue3 == 3)
{
System.out.print("Three of ");
}
if (faceValue3 == 4)
{
System.out.print("Four of ");
}
if (faceValue3 == 5)
{
System.out.print("Five of ");
}
if (faceValue3 == 6)
{
System.out.print("Six of ");
}
if (faceValue3 == 7)
{
System.out.print("Seven of ");
}
if (faceValue3 == 8)
{
System.out.print("Eight of ");
}
if (faceValue3 == 9)
{
System.out.print("Nine of ");
}
if (faceValue3 == 10)
{
System.out.print("Ten of ");
}
if (faceValue3 == 11)
{
System.out.print("Jack of ");
}
if (faceValue3 == 12)
{
System.out.print("Queen of ");
}
if (faceValue3 == 13)
{
System.out.print("King of ");
}
if (suit3 == 1)
{
System.out.print("Spades");
System.out.println();
}
if (suit3 == 2)
{
System.out.print("Hearts");
System.out.println();
}
if (suit3 == 3)
{
System.out.print("Diamonds");
System.out.println();
}
if (suit3 == 4)
{
System.out.print("Clubs");
System.out.println();
}
System.out.println("Your hand is " + faceValue1 + suit1 +"," + faceValue2 + suit2 + "," + faceValue3 + suit3);
}
//This method will determine if the player has a flush
public static int flush(String main)
{
int suit2;
int suit1;
for (int counter = 1; counter < 3; counter++)
{
if (suit1 != suit2)
{
return 1;
}
}
return 0;
}
//This method will determine if the player has a straight
public static int straight(String main)
{
int card1;
int card2;
for (int counter2 = 1; counter2 < 3; counter2++)
if (card1[counter2 - 1] != (card2[counter2] - 1))
{
return 1;
}
return 1;
}
//This method will determine if the player has three of a kind
public static int threekind(String main)
{
int card1;
int card2;
int card3;
if (card1 == card2 && card2 == card3)
{
return 1;
}
return 0;
}
//This method will determine if the player has a pair
public static int pair(String main)
{
int check = 0;
int card1;
int card2;
for(int counter = 1; counter <3; counter++)
{
if (card1[counter - 1] == card2[counter])
{
check++;
}
}
if (check == 1)
{
return 1;
}
else
{
return 0;
}
}
//This method will tell the player if he has a high card
public static int highcard(String main)
{
int highcard = 0;
int card1;
int card2;
for (int counter = 0; counter < 3; counter++)
{
if (card1[counter] > highcard)
{
highcard = card1[counter];
}
}
return highcard;
}
/* This method will evaluates the hand and see what the player has
and give the player an output of their hand*/
public static int evaluate(String main)
{
if (flush() == 1)
{
System.out.println("You have a flush!");
}
else if (straight() == 1)
{
System.out.println("You have a straight!");
}
else if (threekind() == 1)
{
System.out.println("You have a three of a kind!");
}
else if (pair() == 1)
{
System.out.println("You have a pair!");
}
else
{
int highCard = highCard();
System.out.println("Your highest card is " + highCard);
}
}
}
}}
以下是整套代码
class MyClass {
private boolean timeUp = false;
public void nextTuple() {
if (timeUp) {
EmitMyTuple();
ResetTimeCounterToZero();
timeUp = false;
} else {
boolean greater = CheckIfTimeIsGreaterThanOneSecond();
if (greater) {timeUp = true;}
}
}
SleepForFewMilliseconds();
}
答案 0 :(得分:0)
我认为这条线不会抛出这样的错误
for(int counter = 1; counter <3; counter++)
但这三个肯定会有问题
if (card1[counter - 1] == card2[counter])
...
if (card1[counter] > highcard) // error here
{
highcard = card1[counter]; // error here
因为你试图迭代card1和card2,它们是整数而不是数组
答案 1 :(得分:0)
在所有这三种情况中,card1
和card2
都不是数组。它们是直的int
。订阅int
没有意义(它甚至意味着什么?)。