Java:大括号在if-then-else中起作用

Java:大括号在if-then-else中起作用,java,Java,在下面的程序中,如果我没有将大括号与if-then-else语句放在一起,我将执行所有语句,结果扫描器输入询问我3次问题 import java.util.Scanner; public class NestedIf { public static void main (String [] args) { System.out.println ("Who is the presiden of USA?"); String response =

在下面的程序中,如果我没有将大括号与if-then-else语句放在一起,我将执行所有语句,结果扫描器输入询问我3次问题

import java.util.Scanner;

public class NestedIf
{
    public static void main (String [] args)
    {

        System.out.println ("Who is the presiden of USA?");

        String response ="";
        String answer;

        answer = "Obama";

        Scanner input = new Scanner (System.in);
        response = input.nextLine();

        if (response.equals(answer))
            System.out.println ("That is right");
        else  
            System.out.println ("That is wrong");
            response = input.nextLine();

            if (response.equals(answer))
            System.out.println ("That is right");
            else 
                System.out.println ("That is wrong");
                response = input.nextLine();

                if (response.equals(answer))
                System.out.println ("That is right");
                else 
                System.out.println ("That is wrong - Bye");


    }
}
结果是:

谁是美国总统? 奥巴马 没错 奥巴马 没错 奥巴马 没错

如果我用大括号将程序设置为如下所示,程序会询问问题,当我回答“更正”时,其他else语句不会执行

import java.util.Scanner;

public class NestedIf
{
    public static void main (String [] args)
    {

        System.out.println ("Who is the presiden of USA?");

        String response ="";
        String answer;

        answer = "Obama";

        Scanner input = new Scanner (System.in);
        response = input.nextLine();

        if (response.equals(answer))
            System.out.println ("That is right");
        else  **{**
            System.out.println ("That is wrong");
            response = input.nextLine();

            if (response.equals(answer))
            System.out.println ("That is right");
            else **{**
                System.out.println ("That is wrong");
                response = input.nextLine();

                if (response.equals(answer))
                System.out.println ("That is right");
                else 
                System.out.println ("That is wrong - Bye");
            **}**
        **}**
    }
}
结果是:
谁是美国总统?
奥巴马
没错


我的问题是为什么大括号在这里起作用,尽管我在这个论坛上读到大括号没有任何作用

无大括号:if仅用于下一行。
大括号:If语句对大括号之间的所有内容都有效

无大括号:if仅用于下一行。
大括号:If语句对大括号之间的所有内容都有效

无大括号:if仅用于下一行。
大括号:If语句对大括号之间的所有内容都有效

无大括号:if仅用于下一行。
大括号:If语句对大括号之间的所有内容都有效

> P>如果你没有定义括号,如果是,否则,它只会考虑一个语句。 您的代码在逻辑上与以下代码相同:

import java.util.Scanner;

public class NestedIf
{
    public static void main (String [] args)
    {

        System.out.println ("Who is the presiden of USA?");

        String response ="";
        String answer;

        answer = "Obama";

        Scanner input = new Scanner (System.in);
        response = input.nextLine();

        if (response.equals(answer))
        {
            System.out.println ("That is right");
        }
        else
        {  
            System.out.println ("That is wrong");
        }
            response = input.nextLine();

            if (response.equals(answer))
            {
                System.out.println ("That is right");
            }
            else
            { 
                System.out.println ("That is wrong");
            }
                response = input.nextLine();

                if (response.equals(answer))
                System.out.println ("That is right");
                else 
                System.out.println ("That is wrong - Bye");


    }
}

如果你不在后面定义括号,否则,它只会考虑一个语句。 您的代码在逻辑上与以下代码相同:

import java.util.Scanner;

public class NestedIf
{
    public static void main (String [] args)
    {

        System.out.println ("Who is the presiden of USA?");

        String response ="";
        String answer;

        answer = "Obama";

        Scanner input = new Scanner (System.in);
        response = input.nextLine();

        if (response.equals(answer))
        {
            System.out.println ("That is right");
        }
        else
        {  
            System.out.println ("That is wrong");
        }
            response = input.nextLine();

            if (response.equals(answer))
            {
                System.out.println ("That is right");
            }
            else
            { 
                System.out.println ("That is wrong");
            }
                response = input.nextLine();

                if (response.equals(answer))
                System.out.println ("That is right");
                else 
                System.out.println ("That is wrong - Bye");


    }
}

如果你不在后面定义括号,否则,它只会考虑一个语句。 您的代码在逻辑上与以下代码相同:

import java.util.Scanner;

public class NestedIf
{
    public static void main (String [] args)
    {

        System.out.println ("Who is the presiden of USA?");

        String response ="";
        String answer;

        answer = "Obama";

        Scanner input = new Scanner (System.in);
        response = input.nextLine();

        if (response.equals(answer))
        {
            System.out.println ("That is right");
        }
        else
        {  
            System.out.println ("That is wrong");
        }
            response = input.nextLine();

            if (response.equals(answer))
            {
                System.out.println ("That is right");
            }
            else
            { 
                System.out.println ("That is wrong");
            }
                response = input.nextLine();

                if (response.equals(answer))
                System.out.println ("That is right");
                else 
                System.out.println ("That is wrong - Bye");


    }
}

如果你不在后面定义括号,否则,它只会考虑一个语句。 您的代码在逻辑上与以下代码相同:

import java.util.Scanner;

public class NestedIf
{
    public static void main (String [] args)
    {

        System.out.println ("Who is the presiden of USA?");

        String response ="";
        String answer;

        answer = "Obama";

        Scanner input = new Scanner (System.in);
        response = input.nextLine();

        if (response.equals(answer))
        {
            System.out.println ("That is right");
        }
        else
        {  
            System.out.println ("That is wrong");
        }
            response = input.nextLine();

            if (response.equals(answer))
            {
                System.out.println ("That is right");
            }
            else
            { 
                System.out.println ("That is wrong");
            }
                response = input.nextLine();

                if (response.equals(answer))
                System.out.println ("That is right");
                else 
                System.out.println ("That is wrong - Bye");


    }
}

Java编译器不关心缩进

因此,需要在代码块周围添加{和}

if (something)
    instruction1
    instruction2
编译器将理解为:

if (something) {
    instruction1
}
instruction2

Java编译器不关心缩进

因此,需要在代码块周围添加{和}

if (something)
    instruction1
    instruction2
编译器将理解为:

if (something) {
    instruction1
}
instruction2

Java编译器不关心缩进

因此,需要在代码块周围添加{和}

if (something)
    instruction1
    instruction2
编译器将理解为:

if (something) {
    instruction1
}
instruction2

Java编译器不关心缩进

因此,需要在代码块周围添加{和}

if (something)
    instruction1
    instruction2
编译器将理解为:

if (something) {
    instruction1
}
instruction2

如果没有大括号,只有紧跟在If后面的第一个语句才被视为If的一部分。

如果没有大括号,只有紧跟在If后面的第一个语句才被视为If的一部分。

如果没有大括号,只有紧跟在If后面的第一个语句才被视为If的一部分。

如果没有大括号,只有紧跟在If后面的第一个语句才被视为If的一部分。
如果没有大括号,则
if
else
子句将只包含一条语句。 如果希望
if
else
子句包含多个语句,则需要使用大括号

您已经缩进了源代码,就好像您希望对
else
子句执行多个语句一样,由于缩进,Java应该“刚好知道”。但实际上,Java编译器并不关心缩进

如果不使用大括号将语句括在同一分支中,编译器会认为您的程序如下所示:

    if (response.equals(answer))
        System.out.println ("That is right");
    else  
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong - Bye");

如果没有大括号,
if
else
子句将只包含一条语句。 如果希望
if
else
子句包含多个语句,则需要使用大括号

您已经缩进了源代码,就好像您希望对
else
子句执行多个语句一样,由于缩进,Java应该“刚好知道”。但实际上,Java编译器并不关心缩进

如果不使用大括号将语句括在同一分支中,编译器会认为您的程序如下所示:

    if (response.equals(answer))
        System.out.println ("That is right");
    else  
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong - Bye");

如果没有大括号,
if
else
子句将只包含一条语句。 如果希望
if
else
子句包含多个语句,则需要使用大括号

您已经缩进了源代码,就好像您希望对
else
子句执行多个语句一样,由于缩进,Java应该“刚好知道”。但实际上,Java编译器并不关心缩进

如果不使用大括号将语句括在同一分支中,编译器会认为您的程序如下所示:

    if (response.equals(answer))
        System.out.println ("That is right");
    else  
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong - Bye");

如果没有大括号,
if
else
子句将只包含一条语句。 如果希望
if
else
子句包含多个语句,则需要使用大括号

您已经缩进了源代码,就好像您希望对
else
子句执行多个语句一样,由于缩进,Java应该“刚好知道”。但实际上,Java编译器并不关心缩进

如果不使用大括号将语句括在同一分支中,编译器会认为您的程序如下所示:

    if (response.equals(answer))
        System.out.println ("That is right");
    else  
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong");
    response = input.nextLine();

    if (response.equals(answer))
        System.out.println ("That is right");
    else 
        System.out.println ("That is wrong - Bye");

默认情况下,<强> JVM < /强>考虑具有范围的每个循环语句 就一行

您可以使用
{}
符号

因此,通过这样做,您可以将函数的范围扩大到更大的范围 区域及其下的所有代码将被视为 头

例如:

如果你有这样的If语句

   if (someCondition)
            statement 1;
   else  
       Statement 2
   statement 3;
现在,
if(someCondition)
have
语句1在其范围内,而
else
在其范围内有
语句2
语句3在任何情况下都不在
if
else
的范围内

现在可以增加th