如何使main方法中的变量在begin方法上工作? [英] How do i make the variables from main method work on begin method?

查看:143
本文介绍了如何使main方法中的变量在begin方法上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让main方法的用户输入在begin方法上工作,以在画布中创建一个框的大小,这样当使用不同的输入时它会创建不同的大小
这就是我所拥有的到目前为止:

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);
    }
}       


推荐答案

定义它们在您的构造函数中,以便它们可用于实例。
变量在本地方法范围内可用。

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.

要使实例方法begin()可以访问您的变量,您可以做3件事:

To make your variable accessible to the instance method begin() you can do 3 things:

1:创建静态变量以保存这些值。这使它们全球可用

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:创建一个实例变量来保存此值并使用属性扩展构造函数将它们传递给实例

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:展开方法签名以包含其他参数。在您的情况下,您不要调用begin(),因此这不是一个选项。但如果它是你控制的方法那么类似

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
    ...
}

这篇关于如何使main方法中的变量在begin方法上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆