Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java for循环语法有什么问题?_Java_Loops_For Loop_Statements - Fatal编程技术网

Java for循环语法有什么问题?

Java for循环语法有什么问题?,java,loops,for-loop,statements,Java,Loops,For Loop,Statements,我这里有一段代码-PlayingDeck类。正如您所看到的,它没有完成,但是在第17行,for循环的正下方,我收到一条错误消息,说PlayingCard声明和初始化不是一条语句。为什么会这样?当我使用相同的语句将花括号添加到for循环时,错误消息消失。有人能解释一下原因吗?我只需要两个括号,一个在for循环的开始,一个在结束 多谢各位 不编译的代码-------- 编译的代码----- 这是不允许的,因为如果不使用大括号将PlayingCard currentCard的声明括起来,则该声明没有作

我这里有一段代码-
PlayingDeck
类。正如您所看到的,它没有完成,但是在第17行,for循环的正下方,我收到一条错误消息,说
PlayingCard
声明和初始化不是一条语句。为什么会这样?当我使用相同的语句将花括号添加到for循环时,错误消息消失。有人能解释一下原因吗?我只需要两个括号,一个在for循环的开始,一个在结束

多谢各位

不编译的代码--------

编译的代码-----


这是不允许的,因为如果不使用大括号将
PlayingCard currentCard
的声明括起来,则该声明没有作用域


顺便说一句,如果你想让你的循环做点什么,你可能应该把条件改成
currentCardNumber<51

您的问题在
for循环中

for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++)
            PlayingCard currentCard = playingDeckArray[currentCardNumber];
}
缺少开头的大括号
{

这是任何语言的标准,不仅仅是java。任何循环,无论是
for loop
,或
while loop
do while loop
,或任何类似
if else
if else if-else
switch
的块,都必须有打开和关闭的花括号

有关更多详细信息,请阅读以下内容:


还请阅读以下内容:

公平地说,它有一个作用域,但该作用域是无用的吗?或者甚至将条件更改为
currentCardNumber
。我想这是有意义的,因为当我对循环执行此操作时,一切都编译得很好:
PlayingDeck(){DeckArray=new PlayingCard[NumoFaits][NumoFlanks];对于(int currentSuitNumber=1;currentSuitNumber)
public class PlayingDeck {
    static int numberOfCards;

    static {
        numberOfCards = 52;
    }

    PlayingCard[] playingDeckArray;

    PlayingDeck() {
        playingDeckArray = new PlayingCard[numberOfCards];

        for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++){
            PlayingCard currentCard = playingDeckArray[currentCardNumber];
        }
    }

    public static void main(String[] args) {

    }
}
for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++)
            PlayingCard currentCard = playingDeckArray[currentCardNumber];
}