首页 > 解决方案 > 如何使其他方法可以访问对象?

问题描述

我正在为我的学校做一个存储库存计划。我只是不完全理解它,因为我缺乏编程知识,所以我通常只是在 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”,因为它没有在范围内声明。我不知道在外面声明它

标签: javaobjectarraylist

解决方案


如果我猜对了,请回答您的问题:您的 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();
        }
  //            ...
    }
}

}


推荐阅读