从另一个具有自己变量的类调用方法

时间:2017-02-06 09:28:49

标签: java methods

所以我试图从CustomerTest类中的printDetails()类调用我的方法Customer。没有记录变量的值,当我运行它时,我最终得到名称的null和数组的错误。我不确定如何获取要记录和打印的变量值。

public class Customer {   
    public String name;
    public int[] itemCost;    
    public void printDetails(){
        System.out.print("Great, here is your customer's purchase details: \n");
        System.out.print("Name:"+name);
        System.out.println();
        for (int i = 0; i < itemCost.length; i ++){
            System.out.print("Item Cost #" + (i+1) + " : ");
            System.out.print(itemCost[i] + "\n");       
            }
        int sum = IntStream.of(itemCost).sum();
        System.out.println("Total:" + sum);
    }
}

public class CustomerTest {
    public static void main(String[] args){
        Customer main = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
            }
        main.printDetails();
        }
    }
}

6 个答案:

答案 0 :(得分:0)

您可以将扫描仪中的值保存在本地变量中,而不是将它们添加到客户的变量中。

应该是这样的:

    Customer main = new Customer();
    Scanner scan = new Scanner(System.in);
    System.out.print("Do you want to create a customer? \n");
    String s = scan.next();
    if(s.equals("y") || s.equals("yes")){
        System.out.print("Ok, what's his/her name? \n");
        main.name = scan.next();    //HERE
        System.out.print("How many items is the customer buying? \n");
        int n = scan.nextInt();
        main.itemCost = new int[n];    //HERE
        for (int i = 0; i < main.itemCost.length; i ++){    //HERE
            System.out.print("Enter a value for item #"+(i+1) );
            System.out.printf("%n");
            int j = scan.nextInt();                              
            main.itemCost[i] = j;    //AND HERE
        }
    main.printDetails();
    }

答案 1 :(得分:0)

您需要为Customer类的实例分配值。

示例代码

 public class CustomerTest {
    public static void main(String[] args){
        Customer main = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            main.name = name;             //added to assign value of name to Custommer's name.
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
             main.itemCost = new int[n];   //Here also need to initialize size for array.
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
                main.itemCost[i] = j; //added to assign value of itemCost to Custommer's itemCost.
            }
        main.printDetails();
        }
    }
}

现在您无法获得nameitemCost的空值。

希望它会对你有所帮助。

答案 2 :(得分:0)

Setters班级中使用Customer 方法,这样您就可以设置从CustomerTestCustomer类变量的值。像这样:

客户类:

public class Customer {   
public String name;
public int[] itemCost;    
public void printDetails(){
    System.out.print("Great, here is your customer's purchase details: \n");
    System.out.print("Name:"+name);
    System.out.println();
    for (int i = 0; i < itemCost.length; i ++){
        System.out.print("Item Cost #" + (i+1) + " : ");
        System.out.print(itemCost[i] + "\n");       
        }
    int sum = IntStream.of(itemCost).sum();
    System.out.println("Total:" + sum);
}
/**
 * Your Setters method...
 */
public void setName(String name){
    this.name = name;
}
public void setItemCost(int[] itemCost){
    this.itemCost= itemCost;
}
}

之后,您可以在CustomerTest中使用它,如下所示:

CustomerTest Class:

// In the End part of your CustomerTestClass
main.setName(name);
main.setItemCost(itemCost);
main.printDetails();

答案 3 :(得分:0)

您在CustomerTest中创建了与Customer类中相同的变量。

这不起作用,因为这些本地 - CustomerTest类变量与实际的Customer对象属性之间没有绑定。

您需要执行以下操作才能真正让更改反映到所有地方:

class CustomerTest{

    public static void main(){
    ...
    ...
    Scanner sc = new Scanner();
    Customer obj = new Customer();
    obj.name = sc.next();
    ...
    ...
    //Test print function
    obj.printDetails();
    }
}

这样可行! 要记住:在类之间创建相同的名称变量不会在对象之间保持值!

答案 4 :(得分:0)

您可以使用setter和getter为客户的nameitemCost设置值,然后调用printDetails()方法,使用此方法时,您必须为每个方法创建客户对象,另一方面,您可以按printDetails()的{​​{1}}参数传递值,然后使用public void printDetails(String name, int[] itemCost) { // same logic here }

调用它

答案 5 :(得分:0)

您可以使用name和itemCost声明Customer构造函数作为参数,例如

MPAndroidchart

在测试类中,您可以在用户输入后构建Customer对象,如此

public Customer(String name, int[] itemCost){
    this.name = name;
    this.itemCost = itemCost;
}