![](/img/trans.png)
[英]How do I take an user input from Scanner in one class and then apply it to another class in the same package?
[英]How do I take an input from my scanner from one class and put it in another class
我是一名高中生,对 Java 非常陌生。 我在面向对象编程方面有点挣扎。 我不确定如何从扫描仪获取输入并将输入实现到我的代码中。 如果我的问题不好,我很抱歉,我只是不确定如何解决这个问题,因为我使用的教科书太复杂了。
这是我的主文件:
import java.io.*;
import java.util.*;
//for best results play in code screen
public class Main {
public static void main(String[] args) {
// calls on the gameStructure file automaticlly so the player wont have to do it
// manually
new GameStructure();
Scanner reader = new Scanner(System.in);
System.out.println("How long do you want your snake to be? For best results make it less then 7 :)?");
public int rad = reader.nextInt();
}
}
这是我的第二个文件 GamePanel.java
int bodyParts = parts.rad;
这是我尝试调用扫描仪输入的那一行 这是我得到的错误:
int bodyParts = parts.rad;
^
symbol: variable rad
location: variable parts of type Main
1 error
exit status 1
^C
您需要将其作为参数传递给另一个类中的方法,或者将其作为静态类属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.