如何访问实例& if else语句中的变量

时间:2013-08-25 08:30:45

标签: java

如何访问变量' user_input' &安培;实例' c1'?根据我的理解,它们在if-else语句@ choice 1的范围之外是不可访问的。

请参阅以下内容:

 public static void main (String [] args)
{
    int choice;
    int user_input;

    Scanner in = new Scanner (System.in);

    do
    {
        System.out.println ("==== Circle Computation =========");
        System.out.println ("| 1. Create a new circle\t|");
        System.out.println ("| 2. Print area\t\t\t|");
        System.out.println ("| 3. Print circumference\t|");
        System.out.println ("| 4. Quit\t\t\t|");

        System.out.println ("Choose option (1 - 3): ");
        choice = in.nextInt();

        if (choice == 1)
        {
            System.out.println ("Enter the radius to compute the area & circumference:");
            user_input = in.nextInt();              

            Circle c1 = new Circle(user_input);     //refer to prob @ choice '2'
            System.out.println ("A new circle is created.\n");
        }

        if (choice == 2)
        {           
            //Prob - instance 'c1' & var 'user_input' @ choice '1' not visible in other if-else statements?
            //http://stackoverflow.com/questions/11685023/creating-an-instance-of-an-object-within-an-if-in-c-sharp
            System.out.println ("Area of circle:");
            System.out.println ("Radius: " + c1.getRadius());
            System.out.println ("Area: " + c1.area());
        }                   

4 个答案:

答案 0 :(得分:2)

Circle c1if语句中定义,因此只能在那里访问。

user_input在while循环之外定义,因此可以在整个循环中访问

每个变量都有definition scope。当块中定义的变量c(在{}之间)时,它仅限于该范围,这意味着它只能在该范围内引用。

答案 1 :(得分:1)

变量user_input在if条件下均可访问,因为其范围为main方法

声明

if (choice == 1)
{
    System.out.println ("Enter the radius to compute the area & circumference:");
    user_input = in.nextInt();              

    Circle c1 = new Circle(user_input);     //refer to prob @ choice '2'
    System.out.println ("A new circle is created.\n");
}

c1的范围是括号,如果条件。如果您希望在其他if循环中访问它,那么您可能希望将c1移出如下所示。我已将c1的范围更改为main方法,以便它符合您的逻辑。

    Circle c1 = null;
    do
    {
        System.out.println ("==== Circle Computation =========");
        System.out.println ("| 1. Create a new circle\t|");
        System.out.println ("| 2. Print area\t\t\t|");
        System.out.println ("| 3. Print circumference\t|");
        System.out.println ("| 4. Quit\t\t\t|");

        System.out.println ("Choose option (1 - 3): ");
        choice = in.nextInt();
        if (choice == 1)
        {
            System.out.println ("Enter the radius to compute the area & circumference:");
            user_input = in.nextInt();              

            c1 = new Circle(user_input);     //refer to prob @ choice '2'
            System.out.println ("A new circle is created.\n");
        }

        if (choice == 2)
        {           
            //Prob - instance 'c1' & var 'user_input' @ choice '1' not visible in other if-else statements?
            //http://stackoverflow.com/questions/11685023/creating-an-instance-of-an-object-within-an-if-in-c-sharp
            System.out.println ("Area of circle:");
            System.out.println ("Radius: " + c1.getRadius());
            System.out.println ("Area: " + c1.area());
        }           
    }

答案 2 :(得分:0)

user_input 变量创建到主方法块
中 所以它可以访问if或else阻止。 但 C1 无法访问,因为它是IF块的本地变量/实例

答案 3 :(得分:-2)

User_input可以在整个方法中使用。

C1不能用于第二部分