2014-04-25 91 views
0

我在我的java程序中有2个类,第一个类包含一个文本字段,我在其中获取用户输入。我试图通过一个int值来自一个文本字段(用户输入)并将该值存储在另一个类中包含的数组中。从类传递一个int值到另一个,并将其存储在一个Java数组中

1类 这是第一类是我从一个文本字段获取数据

if (SubmitButton.equals(e.getSource())) { 
    int input = Integer.parseInt(textField1.getText()); 
    myClass2.setAge(input); 
} 

2类 这是我试图从用户输入的数据从1类存储到类数组

public int setAge(int input) {  
    return input; 
} 
//this is where i am trying to store the value 
int[] Age = {input}; 

出于某种原因,“输入”不能被解析为一个变量

一些能帮助一个认罪SE?

回答

1

因为,可变input有方法的局部范围内,你不能从方法setAge()

1

使用input变量,实例成员变量

0
int input; 

public int setAge(int input) { 
     this.input=input; 
      return input; 
     } 
//this is where i am trying to store the value 
int[] Age = {input}; 
0

在你的第二类外部访问它,定义数组作为成员变量并分配值。

public class MyClass2 { 

    private int[] age = new int[1]; 

    public void setAge(int input) { 
     age[0] = input; 
    } 

} 
0

我想你想在Class2中存储“输入”,然后把它放到Age数组。为此,你应该在类2中有一个名为“输入”的变量(我猜你的名字更喜欢这个)。之后你可以像这样存储这些数据;

private int input; 
public int setAge(int input) {  
    this.input = input; 
    return input; 
} 
int[] Age = {input}; 
相关问题