【问题标题】:Assign a variable inside an "if" bracket, how do you use that variable outside the bracket?在“if”括号内分配一个变量,如何在括号外使用该变量?
【发布时间】:2011-06-30 08:21:03
【问题描述】:

我正在制作一个制作分形的程序,并且假设用户可以选择分形的颜色。唯一的问题是颜色变量在“if”语句之外不起作用。这是我的代码:(public void fractalEngine 中的问题在于设置笔颜色的位置。那些verifiedChoice 变量在 if 语句中启动,似乎不会延续到代码的其余部分。)

import java.awt.*;
import java.util.Scanner;

class anotherClass
{
    World worldObj = new World();
    Turtle m = new Turtle(100, 340, worldObj);
    Scanner in = new Scanner(System.in);

    public void hello()
    {
        System.out.println("This program is a fractal engine");
        System.out.println("A fractal shape is a geometric shape that represents"); 
        System.out.println("the whole shape, no matter what level of scale");
        System.out.println();
        System.out.println("Your fractal picture will have three different colors,");
        System.out.println("Please pick three of the following:");
        System.out.println("RED, GREEN, BLUE, ORANGE, BLACK, YELLOW, MAGENTA, PINK");
        System.out.println("NOTE: Type your choices just like they appear.");
        System.out.println();
    }


    public void fractalEngine(int pxLength, String fractalRule)
    {
        System.out.println("Choice #1 out of 3: ");
        String choice1 = in.nextLine();
        if(choice1.equals("RED") || choice1.equals("GREEN") || choice1.equals("BLUE") || choice1.equals("ORANGE") || choice1.equals("BLACK") || choice1.equals("YELLOW") || choice1.equals("MAGENTA") || choice1.equals("PINK"))
        {
            System.out.println("Your first choice has been set to " + choice1);
            String verifiedChoice1 = choice1;
        }
        else
        {
            System.out.println("That choice is not valid, your first choice will now be red");
            String verifiedChoice1 = "RED";
        }



        System.out.println("Choice #2 out of 3: ");
        String choice2 = in.nextLine();
        if(choice2.equals("RED") || choice2.equals("GREEN") || choice2.equals("BLUE") || choice2.equals("ORANGE") || choice2.equals("BLACK") || choice2.equals("YELLOW") || choice2.equals("MAGENTA") || choice2.equals("PINK"))
        {
        System.out.println("Your second choice has been set to " + choice2);
        String verifiedChoice2 = choice2;
        }
        else
        {
            System.out.println("That choice is not valid, your second choice will now be green");
            String verifiedChoice2 = "GREEN";
        }



        System.out.println("Choice #3 out of 3: ");
        String choice3 = in.nextLine();
        if(choice3.equals("RED") || choice3.equals("GREEN") || choice3.equals("BLUE") || choice3.equals("ORANGE") || choice3.equals("BLACK") || choice3.equals("YELLOW") || choice3.equals("MAGENTA") || choice3.equals("PINK"))
        {
            System.out.println("Your thrid choice has been set to " + choice3);
            String verifiedChoice3 = choice3;
        }
        else
        {
            System.out.println("That choice is not valid, your thrid choice will now be blue");
            String verifiedChoice3 = "BLUE";
        }

        m.setHeading(0);
        String subData = "";   
        m.turn(30);
        for(int n = 0; n < fractalRule.length() ; n=n+1)            
        {
            subData = fractalRule.substring(n, n+1);                 

            if(subData.equalsIgnoreCase("F"))
                m.forward(pxLength);
            else if(subData.equals("+"))
                m.turn(120);
            else if(subData.equals("-"))
                m.turn(300);
            else if(subData.equals("1"))
                m.setPenColor(verifiedChoice1);
            else if(subData.equals("2"))
                m.setPenColor(verifiedChoice2);
            else if(subData.equals("3"))
                m.setPenColor(verifiedChoice3);
            else if(subData.equalsIgnoreCase("Q"))              
                m.hide();

        }
    }
}

public class complexFractal
{

    public static void main(String[] args)
    {
       int lineLength = 15;
       String rule = "1F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F-F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F-F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F2+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F-F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F-F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F3+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F-F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-F-F-F+F-F-F-F+F-F+F-F+F-F-F-F+F-FQ";

       anotherClass ac = new anotherClass();

       ac.hello();
       ac.fractalEngine(lineLength, rule);
    }
}

【问题讨论】:

    标签: java variables if-statement brackets assign


    【解决方案1】:

    在您的public void fractalEngine(int pxLength, String fractalRule) 中,只需在{ 符号之后 声明局部变量:

    public void fractalEngine(int pxLength, String fractalRule) {
        String verifiedChoice1  = "";
        String verifiedChoice2  = "";
        String verifiedChoice3  = "";
    
        //Logic here..
    
    }
    

    这些局部变量仅在方法块内部可见,在方法外部不可见。

    只要确保删除您在if 语句中提到的String 字词,它就可以在您的方法中本地使用。

    【讨论】:

    • 正确。但是,您可能需要补充一点,该变量无法从外部访问,因为变量的范围仅限于它的块。
    • 谢谢!!我一直盯着屏幕好几个小时试图弄清楚这一点!它就像一个魅力! :D
    • @EinLama,是的,因此我说它是一个局部变量。我假设 OP 理解局部变量的含义。
    • @Chris,不客气。不要忘记接受对您有帮助的答案。 :)
    • @精英绅士我去办了,但是说要等5分钟,那我就接受了。
    【解决方案2】:

    这是一个范围问题。您需要将声明从 if 子句中提取出来,并将其与实例化分开。您可能还需要将其初始化为 null 以停止 Java 抱怨。

    例如:

    function() {
      String verifiedChoice = null;
    
      if {
        verifiedChoice = ...
      }
    }
    

    有些人更喜欢用 "" 进行初始化,但我选择 null,因为如果你忘记实例化它之后你会得到 NPE 异常与 String 对象交互。

    【讨论】:

      【解决方案3】:

      你也可以这样做:

      String input = null;
      if((input = in.nextLine()).equals("RED")) {
      // do something here
      }
      

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 2019-06-10
        • 1970-01-01
        • 2014-02-21
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 2014-05-31
        • 2015-03-18
        相关资源
        最近更新 更多