Java: Call a method inside a method and use the same variables
I have this code which uses an open source API (Sweethome3D). What I want to do is to check whether the Room is Parallelogram or Square and then call a method and change the values of the walls and the Room. But if I try to do this, it gives me error. Do you have any idea how I can do that?
@Override
public Home createHome() {
Home home = super.createHome();
if (Shape_of_Room.equals("Parallelogram")) {
float[][] points = { { 0f, 0f }, { 0f, 400f }, { 625f, 400f },
{ 625f, 400f }, { 625f, 0f }, { 0f, 0f } };
Room Parallelogram_Room = new Room(points);
Parallelogram_Room.setAreaVisible(true);
// Add new Room to Home
home.addRoom(Parallelogram_Room);
// Check for Ceiling and Floor Display
if (Floor_Display.equals("true")) {
Parallelogram_Room.setFloorVisible(true);
}
else if (Floor_Display.equals("false")) {
Parallelogram_Room.setFloorVisible(false);
}
if (Ceiling_Display.equals("true")) {
Parallelogram_Room.setCeilingVisible(true);
}
else if (Ceiling_Display.equals("false")) {
Parallelogram_Room.setCeilingVisible(false);
}
Wall Left_Wall = new Wall(0, 0, 0, 400, (float) 7.62);
Wall North_Wall = new Wall(0, 400, 625, 400, (float) 7.62);
Wall Right_Wall = new Wall(625, 400, 625, 0, (float) 7.62);
Wall South_Wall = new Wall(625, 0, 0, 0, (float) 7.62);
Left_Wall.setWallAtEnd(North_Wall);
North_Wall.setWallAtEnd(Right_Wall);
Right_Wall.setWallAtEnd(South_Wall);
South_Wall.setWallAtEnd(Left_Wall);
// Check the color of the Walls
// Values set from site
// http://cloford.com/resources/colours/500col.htm
开发者_如何学C // and by the calculator
// http://www.shodor.org/stella2java/rgbint.html
int k;
String[] Color = { "Grey", "Red", "Green", "Blue", "Yellow",
"White" };
int[] Color_Number = { 13882323, 16764108, 7456369, 13421823,
11394815, 16777215 };
for (k = 0; k < 6; k++) {
if (Left_Wall_Color.equals(Color[k])) {
Left_Wall.setLeftSideColor(Color_Number[k]);
}
if (North_Wall_Color.equals(Color[k])) {
North_Wall.setLeftSideColor(Color_Number[k]);
}
if (Right_Wall_Color.equals(Color[k])) {
Right_Wall.setLeftSideColor(Color_Number[k]);
}
if (South_Wall_Color.equals(Color[k])) {
South_Wall.setLeftSideColor(Color_Number[k]);
}
}
// Check if the walls are Matt or Shiny
if (Shiny_Matt.equals("Matt")) {
Left_Wall.setLeftSideShininess(0);
North_Wall.setLeftSideShininess(0);
Right_Wall.setLeftSideShininess(0);
South_Wall.setLeftSideShininess(0);
}
else if (Shiny_Matt.equals("Shiny")) {
Left_Wall.setLeftSideShininess(1);
North_Wall.setLeftSideShininess(1);
Right_Wall.setLeftSideShininess(1);
South_Wall.setLeftSideShininess(1);
}
home.addWall(Left_Wall);
home.addWall(North_Wall);
home.addWall(Right_Wall);
home.addWall(South_Wall);
}
// Modify home as you wish here
System.out.println("method called");
return home;
}
The variables inside a method are not within the scope of your other methods. You have to explicitly pass them as parameters. It's unclear what you want to do with the Home
and Room
objects or which method you would like them to be available in.
The other alternative to passing these objects as method parameters is to store them as class fields; create a private Home home
and private Room room
and assign those fields in your createHome
method. You can then refer to home
and room
inside other methods in the same class. This approach really depends on what you need those variables for, though -- it might not represent a solid object-oriented design.
Some more information on what you want to accomplish would help us to provide better suggestions.
You initialized your variable Home
inside of the method and Room
inside of the if-statement. As soon as Java exits the if-statement or the method, the variables are forgotten instantly. You need to declare them in the root of your class outside of any method:
public class YourClass {
private Home home;
private Room room;
public void createHome() {
home = super.createHome();
// ...
}
}
That doesn't look like valid java code. You are probably getting compiler errors. For every error you get google it and find out exactly what that error means and understand why your code is invalid. That way you'll learn how to write Java code.
You don't show where variables like Shape_of_Room are declared, you're just trying to assign to them. Those should be static variables on the the class level to be able to access them from within the static main method.
精彩评论