将数据从数组传输到不同的方法

时间:2018-03-21 18:52:04

标签: java

我是Java的初学程序员。最近我开始学习如何在单个代码中使用多个方法。现在,我正在尝试创建一个代码,该代码从用户输入数组中获取数据并在单独的方法中使用它以查找输入值的平均值(我计划在代码中添加更多内容,这就是为什么有一个字符串数组,但是现在我想在使代码更复杂之前完全理解我当前的问题)。这是我到目前为止的代码:

import java.util.*;

public class Example{


 public static double namesArray(){
    String[] names={"Employee A", "Employee B", "Employee C", "Employee D", "Employee E", "Employee F", "Employee G", "Employee H", "Employee I", "Employee J"};
 }


 public static double salesArray(){
    Scanner sc = new Scanner(System.in);
    double[] sales = new double[10];
    System.out.println("Enter the sales numbers, in dollars,  for each employee: ");
    for (int i = 0; i < sales.length; i++){
        sales[i] = sc.nextDouble();
    }

    System.out.println(Arrays.toString(names));
    System.out.println(Arrays.toString(sales));

    return sales;

 }
 public static double getAverage(){
    double sum=0;
    for (int i = 0; i < sales.length; i++){
    sum += i;
    }
    double average= sum/sales.length;
    return average;
 }
 public static void main(String[] args){
    namesArray();
    salesArray();
    average();
 }  
}

但是当我编译时,我遇到了这些问题:

Example.java:19: error: cannot find symbol
            System.out.println(Arrays.toString(names));
                                               ^
symbol:   variable names
location: class LabFixing


Example.java:22: error: incompatible types: double[] cannot be converted to double
            return sales;
                   ^

Example.java:27: error: cannot find symbol
            for (int i = 0; i < sales.length; i++){
                                ^
symbol:   variable sales
location: class Example


Example.java:30: error: cannot find symbol
            double average= sum/sales.length;
                                ^
symbol:   variable sales
location: class Example


Example.java:36: error: cannot find symbol
            average();
            ^
symbol:   method average()
location: classExample

5 errors

我认为我遇到的主要问题是我对如何将数组从一种方法传递到另一种方法有点不确定。任何提示或建议将不胜感激,谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题,第一个也是最重要的是您的变量是本地的而不是全局的,这意味着变量只能在本地方法中识别,而不能在整个类中识别。所以,你需要让它们全球化。执行此操作时,您将不需要namesArray()方法。

一旦将这些变量设为全局变量,您的namesArray()和salesArray()就不需要返回任何内容。

接下来,您在main()中调用的最后一个方法不存在。而不是average()尝试getArray()。

记住这一点,试试

import java.util.*;

public class Example{
    static String[] names ={"Employee A", "Employee B", "Employee C", "Employee D", "Employee E", "Employee F", "Employee G", "Employee H", "Employee I", "Employee J"};
    static double[] sales = new double[10];


 public static void salesArray(){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the sales numbers, in dollars,  for each employee: ");
    for (int i = 0; i < sales.length; i++){
        sales[i] = sc.nextDouble();
    }

    System.out.println(Arrays.toString(names));
    System.out.println(Arrays.toString(sales));

 }
 public static double getAverage(){
    double sum=0;
    for (int i = 0; i < sales.length; i++){
    sum += i;
    }
    double average= sum/sales.length;
    return average;
 }
 public static void main(String[] args){
    salesArray();
    System.out.println(getAverage());
 }  
}