java - 如何使其他方法可以访问对象?
问题描述
我正在为我的学校做一个存储库存计划。我只是不完全理解它,因为我缺乏编程知识,所以我通常只是在 youtube 上观看视频。我怎样才能使下面的其他方法可以访问“arraylist 对象”?
package ahaprogram2;
import java.util.Scanner;
public class AhaProgram {
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);`
System.out.println("Hello! This is the AHA Program of Jalosjos, Parreno and Alfonso");
System.out.println("Please type the letter of your option");
boolean loop = false;
while (loop != true) {
showOptions();
inputHandler();
String contInput = reader.nextLine();
if (contInput.equals("1")) {
System.out.println("Input the name of Container 1: ");
String ContInp1 = reader.nextLine();
Container container1 = new Container(ContInp1);
container1.printContainer();
}
if (contInput.equals("2")) {
System.out.println("Input the name of Container 2: ");
String ContInp2 = reader.nextLine();
Container container2 = new Container(ContInp2);
container2.printContainer();
}
if (contInput.equals("3")) {
System.out.println("Input the name of Container 3: ");
String ContInp3 = reader.nextLine();
Container container3 = new Container(ContInp3);
container3.printContainer();
}
if (contInput.equals("4")) {
System.out.println("Input the name of Container 4: ");
String ContInp4 = reader.nextLine();
Container container4 = new Container(ContInp4);
container4.printContainer();
}
if (contInput.equals("5")) {
System.out.println("Input the name of Container 5: ");
String ContInp5 = reader.nextLine();
Container container5 = new Container(ContInp5);
container5.printContainer();
}
}
}
public static void showOptions() {
System.out.println("A = Name Containers");
System.out.println("B = Add Cans");
System.out.println("C = Remove Cans");
System.out.println("D = Display Cans");
System.out.println("E = Quit");
System.out.println("Type a Letter: ");
}
public static void inputHandler() {
Scanner reader = new Scanner(System.in);
String input = reader.nextLine();
if(input.equals("A")) {
System.out.println("There are 5 containers.. What container
will you name? ");
System.out.print("Type the number of your container: ");
}
if (input.equals("B")){
System.out.println("Which container will you use?: ");
System.out.print("Type a number for the container: ");
String contforAdd = reader.nextLine();
if (contforAdd.equals("1")) {
System.out.println("How many cans will you add?: ");
int numofCans1 = Integer.parseInt(reader.nextLine());
for (int i = 0; i < numofCans1; i++) {
System.out.print("Enter the name of Can " + (i+1) + " : ");
String CanName = reader.nextLine();
container1.AddCan();
}
}
}
}
}
这是对象和构造函数的另一个类
import java.util.ArrayList;
import java.util.Scanner;
public class Container {
Scanner reader = new Scanner(System.in);
public ArrayList<String> CanContainer = new ArrayList<String>();
public int Contsizep;
public String contName;
public Container(String contname){
this.contName = contname;
}
public void AddCan(String CantoAdd) {
this.CanContainer.add(CantoAdd);
}
public void printContainer() { // for OPTION A ONLY
System.out.println("NAME SUCCESSFUL: **" + contName +"**");
}
}
他们找不到符号“container1”,因为它没有在范围内声明。我不知道在外面声明它
解决方案
如果我猜对了,请回答您的问题:您的 container1 变量的范围在“main”方法中处于“if”条件。而且这个变量只在这个“if”条件的大括号内可见。如果要将此变量设置为对此类中的其他方法可见,则需要将其设为此类的一个字段。在您的情况下,它必须是静态文件。
public class AhaProgram {
private static Container container1;
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
System.out.println("Hello! This is the AHA Program of Jalosjos, Parreno and Alfonso");
System.out.println("Please type the letter of your option");
boolean loop = false;
while (loop != true) {
showOptions();
inputHandler();
String contInput = reader.nextLine();
if (contInput.equals("1")) {
System.out.println("Input the name of Container 1: ");
String ContInp1 = reader.nextLine();
container1 = new Container(ContInp1);
container1.printContainer();
}
// ...
}
}
}
推荐阅读
- ios - 无法使用 Xcode 12.0 beta 5 构建项目
- python - OpenCV(4.2.0),imwrite() 到子文件夹会破坏模块;cv2.error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'`'
- angular - NGRX 选择器投影仪功能在茉莉花单元测试中返回未定义,角度 10
- c# - 在 Kubernetes 集群内调用 Azure 函数显示错误?
- python - 在 Jupyter Notebook 上运行程序时找不到 os.listdir(path) 路径
- javascript - 递归方法将值更新到顶部
- android - 使用视图绑定和实时数据观察者在 Fragment 中崩溃
- python - 从 csv 中填写缺失的数据
- reactjs - 触发注销自定义钩子
- c# - 将新项目添加到组合框项目源会引发以下异常“'在使用 ItemsSource 之前,Items 集合必须为空。'”