Hello fellow programmers! I am a Java beginner and I need some help.
I have a program that I want to make the Turtle move using prefabricated instructions in a string called "rule". I want that rule to be parsed into a readable format for an if-else statement. The if-else statement will then read every character in the parsed "rule", and make the Turtle move accordingly to each condition.
I have ran into some issues with the program, it parses (I think so) and displays the rule, but there is no movement from the turtle. I believe it has something to do with the if-else statement. Please help!
import java.awt.*;
import java.util.Scanner;
class FractalEngine
{
World worldObj = new World();
Turtle m = new Turtle(200, 200, worldObj);
Scanner in = new Scanner(System.in);
FractalEngine()
{
//String rule = "F-F+F+F-FQ";
String rule = "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";
//String rule = "F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F-F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F-F-F+F+F-F+F-F+F+F-F+F-F+F+F-F-F-F+F+F-F-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";
m.setHeading(0);
Scanner scanner = new Scanner(rule);
System.out.println();
System.out.println("Fractal Rule: ");
System.out.println(scanner.nextLine());
System.out.println();
// it prints it to the terminal, but no Turtle movement. Am I using the right parsing method?
do
{
Scanner scanRule = new Scanner(rule);
scanRule.nextLine();
if(rule.equalsIgnoreCase("F"))
m.forward(25);
else if(rule.equals("-"))
m.turnLeft();
else if(rule.equals("+"))
m.turnRight();
else
rule = "Q";
}
while(!rule.equalsIgnoreCase("Q"));
System.out.println("Fractal construction terminated.");
}
}
public class FractalConstructor
{
public static void main(String[] args)
{
FractalEngine fe = new FractalEngine();
}
}
Aucun commentaire:
Enregistrer un commentaire