我有我在create()方法中创建的对象numberlist,我想访问它,所以我可以在question()方法中使用它.
有没有办法做到这一点,我错过了,或者我只是弄乱了什么?如果没有,我该怎么做才能让我获得与下面相同的功能?
private static void create() {
Scanner input = new Scanner(System.in);
int length,offset;
System.out.print("Input the size of the numbers : ");
length = input.nextInt();
System.out.print("Input the Offset : ");
offset = input.nextInt();
NumberList numberlist= new NumberList(length, offset);
}
private static void question(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a command or type ?: ");
String c = input.nextLine();
if (c.equals("a")){
create();
}else if(c.equals("b")){
numberlist.flip(); \\ error
}else if(c.equals("c")){
numberlist.shuffle(); \\ error
}else if(c.equals("d")){
numberlist.printInfo(); \\ error
}
}
Run Code Online (Sandbox Code Playgroud)