如何使对象可被其他方法访问?

时间:2019-01-10 15:34:32

标签: java object arraylist

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

3 个答案:

答案 0 :(得分:0)

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

}

答案 1 :(得分:0)

变量的范围表示程序中可访问特定变量的部分(要了解Java变量范围的更多信息,建议阅读this文章)。

关于您的直接关注/问题,您可以在以下if语句中创建变量container1

if (contInput.equals("1")) {
    System.out.println("Input the name of Container 1: ");
    String ContInp1 = reader.nextLine();
    Container container1 = new Container(ContInp1);
    container1.printContainer();
}

这意味着container1的范围或程序中其他方法可以访问它的部分仅限于if语句。您的代码尝试访问函数container1中的inputHandler(),该函数不仅在container1的范围之外,而且在实例化container1之前也被调用。为了使container1可以在if语句之外访问,则必须扩大container1的范围。最好的方法是将其设置为AhaProgram类的静态实例变量。看起来像这样:

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();
            } 

          // ...the rest of your program...
}

希望这对您有帮助!

答案 2 :(得分:0)

同意jdv的评论。 为了解决该问题,您现在可以将该变量设置为静态字段。

作为临时解决方案,我建议这样做:

import java.util.Scanner;

公共类AhaProgram {

private static Container container;
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 = new Container(ContInp1);
            container.printContainer();

        }


        if (contInput.equals("2")) {
            System.out.println("Input the name of Container 2: ");
            String ContInp2 = reader.nextLine();
            container = new Container(ContInp2);
            container.printContainer();
        }

        if (contInput.equals("3")) {
            System.out.println("Input the name of Container 3: ");
            String ContInp3 = reader.nextLine();
            container = new Container(ContInp3);
            container.printContainer();
        }

        if (contInput.equals("4")) {
            System.out.println("Input the name of Container 4: ");
            String ContInp4 = reader.nextLine();
            container = new Container(ContInp4);
            container.printContainer();

        }

        if (contInput.equals("5")) {
            System.out.println("Input the name of Container 5: ");
            String ContInp5 = reader.nextLine();
            container = new Container(ContInp5);
            container.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();
                container.AddCan(CanName);
            }

        }

    }

}

}

希望有帮助!