在“if”内部分配变量括号,如何在括号外使用该变量?
我正在制作一个制作分形的程序,用户应该选择分形的颜色。唯一的问题是颜色变量在“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);
}
}
I'm making a program that makes a fractal, and the user is suppose to choose what colors they what the fractal to be. The only problem is that the color variable won't work outside the "if" statement. Here's my code: (Problem in public void fractalEngine
down by where pen colors are being set. Those verifiedChoice
variables are initiated inside an if statement and don't seem to carry over to the rest of the code.)
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的
public void fractalEngine(int pxLength, String fractalRule)
中,只需在{
符号之后声明局部变量:这些局部变量仅可见在方法块内部并且在方法外部不可见。
只需确保删除
if
语句中提到的String
单词,它就可以在您的方法中本地使用。Inside your
public void fractalEngine(int pxLength, String fractalRule)
just declare the local variables right after the{
symbol:These local variables is only visible inside the method block and cannot be visible outside of the method.
Just make sure you remove the
String
word that you mentioned inside yourif
statement and it can be used locally inside your method.这是一个范围问题。您需要将声明从 if 子句中取出,并将其与实例分开。您可能还需要用 null 初始化它以阻止 Java 抱怨。
例如:
有些人喜欢用“”初始化,但我选择 null,因为如果您忘记事后实例化它,您将在事后与 String 对象交互时得到 NPE 异常。
This is a scope problem. You need to pull the declaration out of the if clause and separate that to the instantation. You probably will need to initialise it with null too to stop Java complaining.
For example:
Some people prefer to initialise with "" however I choose null because if you do forget to instantiate it afterwards you'll get NPE exceptions interacting with the String object afterwards.
您还可以这样做:
You can also do this: