问题
I am trying to make the user inputs from the main method work on the begin method to create the size of a box in the canvas so it would create different sizes when different inputs are used This is what i have so far:
import objectdraw.*;
import java.awt.*;
import java.util.Scanner;
public class Ricochet extends WindowController
{
private static final int CANVAS_WIDTH=400;
private static final int CANVAS_HEIGHT=600;
public static void main(String [] args)
{
Scanner scnr = new Scanner(System.in);
System.out.println("Enter size of box in pixels: ");
int boxSize = scnr.nextInt();
System.out.println("Enter number of crossings: ");
int Crossings = scnr.nextInt();
System.out.println("Enter pixel Speed: ");
int pixelSpeed = scnr.nextInt();
new Ricochet().startController(CANVAS_WIDTH, CANVAS_HEIGHT);
}
private FilledRect sq1;
public void begin()
{
sq1 = new FilledRect(1,1, boxSize, boxSize, canvas);
}
}
回答1:
You can pass the required variables from your main method to your begin method as arguments.
If your main method doesn't call begin
directly, you can pass those variables to the constructor of Ricochet
and store them as instance variables.
回答2:
Declare the method with variables in this way:
public static void begin(int boxSize, Canvas canvas)
{
sq1 = new FilledRect(1,1, boxSize, boxSize, canvas);
}
And call it in main:
begin(boxSize, canvas);
回答3:
Define them in your constructor so they are available to the instance. the variables are available in local method scope.
for simplicities sake in the below, i've disregarded inner classes etc.
local variables (in a static method or instance method) are only available inside the method they are declared in.
static variables are available to the class, any static methods and any instance methods.
instance variables are available only to instance methods and initializers of other instance variables.
To make your variable accessible to the instance method begin() you can do 3 things:
1: Create static variables to hold those values. this makes them globally available
private static int boxSize;
public static void main(String[] args) {
...
boxSize = scnr.nextInt(); // static variable
2: Create an instance variable to hold this value and extend the constructor with properties to pass them down to the instance
private int boxSize;
public Ricochet(int inBoxSize) {
this.boxSize = inBoxSize;
}
public static void main(String[] args) {
...
int boxSize = scnr.nextInt(); // local variable
...
new Ricochet(boxSize).startController(CANVAS_WIDTH, CANVAS_HEIGHT);
}
3: expand the method signature to contain the additional arguments. In your case you don't call begin() so this isn't an option. but if it was a method you controlled then something like
public static void main(String[] args) {
...
int boxSize = scnr.nextInt(); // local variable
...
new Ricochet().doSomeMethod(boxSize);
}
public void doSomeMethod(int boxSize) { // add parameter
...
}
来源:https://stackoverflow.com/questions/28086686/how-do-i-make-the-variables-from-main-method-work-on-begin-method