简体   繁体   English

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

[英]Transferring data from arrays to different methods

I am a beginner programmer in Java. 我是Java的初学者。 Recently I started to learn how to use multiple methods in a single code. 最近,我开始学习如何在单个代码中使用多种方法。 Right now, I am trying to create a code that takes data from a user-input array and uses it in a separate method in order to find the average of the inputed values (I plan on adding more to the code, which is why there's a String array of names, but right now I want to fully understand my current issue before making the code more complex). 现在,我正在尝试创建一个代码,该代码从用户输入数组中获取数据,并在单独的方法中使用它,以便找到输入值的平均值(我计划在代码中添加更多的值,这就是为什么一个由名称组成的String数组,但是现在我想在使代码更复杂之前完全了解我当前的问题)。 This is the code I have so far: 这是我到目前为止的代码:

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

But when I compiled, I encountered these issues: 但是当我编译时,遇到了以下问题:

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

I think the main problem I'm having is that I'm a bit unsure of how to pass arrays from one method to another. 我认为我遇到的主要问题是我不确定如何将数组从一种方法传递给另一种方法。 Any tips or advice would be greatly appreciated, thank you for your time. 任何提示或建议,将不胜感激,谢谢您的时间。

There are a couple of problems in your code, the first and the most important being that your variables are local and not global, which means the variables are only recognized inside your local method and not by the entire class. 您的代码中存在两个问题,第一个也是最重要的问题是变量是局部变量,而不是全局变量,这意味着变量只能在局部方法内部而不是整个类中识别。 So, you need to make them global. 因此,您需要使其全球化。 When you do this, you will not need the namesArray() method. 当您这样做时,您将不需要namesArray()方法。

Once you make these variables global, your namesArray() and salesArray() doesn't need to return anything. 一旦将这些变量设置为全局变量,您的namesArray()和salesArray()就不需要返回任何内容。

Next, the last method you invoke in your main() doesn't exist. 接下来,您在main()中调用的最后一个方法不存在。 Instead of average() try getArray(). 请尝试使用getArray()而不是average()。

Keeping that in mind, try 请记住,尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM