Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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 花括号的可用性_Java_Curly Brackets - Fatal编程技术网

Java 花括号的可用性

Java 花括号的可用性,java,curly-brackets,Java,Curly Brackets,假设下面的代码只使用了2个花括号: public void listFish(){ System.out.println(name + " with " + numFishCaught + " fish as follows: "); for (Fish f: fishCaught) if (f != null) System.out.println(f.toString());} 如果我以这种方式重写代码,它会伤害我的代码还是改变它的运行方式?通常使用花括号的正确方法是什么?

假设下面的代码只使用了2个花括号:

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught)
    if (f != null)
    System.out.println(f.toString());}
如果我以这种方式重写代码,它会伤害我的代码还是改变它的运行方式?通常使用花括号的正确方法是什么?谢谢

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught){
    if (f != null){
    System.out.println(f.toString());
    }
}     } 
对于单个语句,它将保持不变,但如果要在if块中组合多个语句,则必须使用大括号。 你应该看到:

块是平衡大括号之间的一组零或多个语句,可以在允许使用单个语句的任何地方使用。以下示例BlockDemo演示了块的使用:

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}
对于单个语句,它将保持不变,但如果要在if块中组合多个语句,则必须使用大括号。 你应该看到:

块是平衡大括号之间的一组零或多个语句,可以在允许使用单个语句的任何地方使用。以下示例BlockDemo演示了块的使用:

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}
对于单个语句,它将保持不变,但如果要在if块中组合多个语句,则必须使用大括号。 你应该看到:

块是平衡大括号之间的一组零或多个语句,可以在允许使用单个语句的任何地方使用。以下示例BlockDemo演示了块的使用:

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}
对于单个语句,它将保持不变,但如果要在if块中组合多个语句,则必须使用大括号。 你应该看到:

块是平衡大括号之间的一组零或多个语句,可以在允许使用单个语句的任何地方使用。以下示例BlockDemo演示了块的使用:

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}

通常,当您创建任何类型的循环并且只有一行代码(即,只有一条语句以分号结尾)时,您不需要{花括号}。但是,如果输入循环将执行多行,则使用{大括号}如下所示:

public void listFish () {
    System.out.println( name + " with " + numFishCaught + " fish as follows: " );
        for ( Fish f: fishCaught ) {
            if ( f != null ) {
                System.out.println( f.toString() );
            }
        }
}
代码是关于它是否可以运行。。。我可以按如下方式重写代码,但它仍然可以完美运行:

public void listFish () { System.out.println( name + " with " + numFishCaught + " fish as follows: " ); for ( Fish f: fishCaught ) { if ( f != null ) { System.out.println( f.toString() ); } } }

排列大括号和其他东西的全部目的是为了可读性。。。如果你能读懂它,你通常就可以去了

通常,当您创建任何类型的循环并且只有一行代码(即,只有一条以分号结尾的语句)时,您不需要{大括号}。但是,如果输入循环将执行多行,则使用{大括号}如下所示:

public void listFish () {
    System.out.println( name + " with " + numFishCaught + " fish as follows: " );
        for ( Fish f: fishCaught ) {
            if ( f != null ) {
                System.out.println( f.toString() );
            }
        }
}
代码是关于它是否可以运行。。。我可以按如下方式重写代码,但它仍然可以完美运行:

public void listFish () { System.out.println( name + " with " + numFishCaught + " fish as follows: " ); for ( Fish f: fishCaught ) { if ( f != null ) { System.out.println( f.toString() ); } } }

排列大括号和其他东西的全部目的是为了可读性。。。如果你能读懂它,你通常就可以去了

通常,当您创建任何类型的循环并且只有一行代码(即,只有一条以分号结尾的语句)时,您不需要{大括号}。但是,如果输入循环将执行多行,则使用{大括号}如下所示:

public void listFish () {
    System.out.println( name + " with " + numFishCaught + " fish as follows: " );
        for ( Fish f: fishCaught ) {
            if ( f != null ) {
                System.out.println( f.toString() );
            }
        }
}
代码是关于它是否可以运行。。。我可以按如下方式重写代码,但它仍然可以完美运行:

public void listFish () { System.out.println( name + " with " + numFishCaught + " fish as follows: " ); for ( Fish f: fishCaught ) { if ( f != null ) { System.out.println( f.toString() ); } } }

排列大括号和其他东西的全部目的是为了可读性。。。如果你能读懂它,你通常就可以去了

通常,当您创建任何类型的循环并且只有一行代码(即,只有一条以分号结尾的语句)时,您不需要{大括号}。但是,如果输入循环将执行多行,则使用{大括号}如下所示:

public void listFish () {
    System.out.println( name + " with " + numFishCaught + " fish as follows: " );
        for ( Fish f: fishCaught ) {
            if ( f != null ) {
                System.out.println( f.toString() );
            }
        }
}
代码是关于它是否可以运行。。。我可以按如下方式重写代码,但它仍然可以完美运行:

public void listFish () { System.out.println( name + " with " + numFishCaught + " fish as follows: " ); for ( Fish f: fishCaught ) { if ( f != null ) { System.out.println( f.toString() ); } } }
排列大括号和其他东西的全部目的是为了可读性。。。如果你能读懂它,你通常就可以去了

不,它不会“伤害”您的代码。实际上,最好的做法是总是使用花括号。解释-找出这四种方法之间的区别:

if (2 == 2)
    System.out.println("First line");
    System.out.println("Second line");


if (2 == 2)
    System.out.println("First line");
System.out.println("Second line");


if (2 == 2) {
    System.out.println("First line");
    System.out.println("Second line");
}


if (2 == 2){
    System.out.println("First line");
}
System.out.println("Second line");
使用大括号时,第一眼就看清楚了。

不,它不会“伤害”您的代码。实际上,最好的做法是总是使用花括号。解释-找出这四种方法之间的区别:

if (2 == 2)
    System.out.println("First line");
    System.out.println("Second line");


if (2 == 2)
    System.out.println("First line");
System.out.println("Second line");


if (2 == 2) {
    System.out.println("First line");
    System.out.println("Second line");
}


if (2 == 2){
    System.out.println("First line");
}
System.out.println("Second line");
使用大括号时,第一眼就看清楚了。

不,它不会“伤害”您的代码。实际上,最好的做法是总是使用花括号。解释-找出这四种方法之间的区别:

if (2 == 2)
    System.out.println("First line");
    System.out.println("Second line");


if (2 == 2)
    System.out.println("First line");
System.out.println("Second line");


if (2 == 2) {
    System.out.println("First line");
    System.out.println("Second line");
}


if (2 == 2){
    System.out.println("First line");
}
System.out.println("Second line");
使用大括号时,第一眼就看清楚了。

不,它不会“伤害”您的代码。实际上,最好的做法是总是使用花括号。解释-找出这四种方法之间的区别:

if (2 == 2)
    System.out.println("First line");
    System.out.println("Second line");


if (2 == 2)
    System.out.println("First line");
System.out.println("Second line");


if (2 == 2) {
    System.out.println("First line");
    System.out.println("Second line");
}


if (2 == 2){
    System.out.println("First line");
}
System.out.println("Second line");

使用大括号时,第一眼就看得很清楚。

只要它能编译,那么它就是关于可读性的。
通常正确的方法是什么
只要可读且一致,就可以按你想要的任何方式。我建议系统地使用大括号。但最重要的是,代码中缺少适当的缩进,以便立即知道每个块属于什么。例如,您的代码没有明确显示System.out.println()调用在if块内。它应该有一个额外的缩进级别。我建议你总是在循环和
if
语句中使用大括号。这将有助于防止以后在代码“block”中插入额外的一行而忘记引入大括号时出现问题,然后就是可读性。
通常正确的方法是什么?
只要可读性和一致性,就可以按照您的意愿进行操作。我建议系统地使用大括号。但最重要的是,代码中缺少适当的缩进,以便立即知道每个块属于什么。例如,你的同事