我正在製作一個分形的程序,用戶可以選擇分形是什麼顏色。唯一的問題是顏色變量不能在「if」語句之外工作。這裏是我的代碼:(。問題在public void fractalEngine
下降,其中筆的顏色被設置這些verifiedChoice
變量內的發起if語句,似乎沒有結轉到代碼的其餘部分)在「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);
}
}
正確。但是,您可能希望添加該變量不能從外部訪問,因爲變量的範圍僅限於該塊。 – EinLama
謝謝!我真的一直盯着屏幕幾個小時試圖弄清楚這一點!它像一個魅力一樣工作! :D – Chris
@EinLama,真的,所以我說這是一個局部變量。我假設OP理解本地變量的含義。 –