简体   繁体   English

将数组作为参数传递给方法

[英]Passing an array as a parameter to a method

Here is my half done program: 这是我完成的程序:

import java.util.*;

public class Sclad {

public static void vavediMasiv(int col[], double cenaE[], double cenaD[]){
    Scanner sc = new Scanner(System.in);
    for (int i = 1; i < col.length; i++)
    {
        System.out.println("Molq vuvedete kolichestvoto ot produkt nomer " + i);
        col[i] = sc.nextInt();
        System.out.println("Kolichestvoto na produkta zadadeno kato: " + col[i]);
    }

    for (int i = 1; i < cenaE.length; i++)
    {
        System.out.println("Molq vuvedete cenata na edro na produkt nomer " + i);
        cenaE[i] = sc.nextDouble();
        System.out.println("Cenata na produkta na edro zadadeno kato: " + cenaE[i]);
    }

    for (int i = 1; i < cenaD.length; i++)
    {
        System.out.println("Molq vuvedete cenata na drebno na produkt " + i);
        cenaD[i] = sc.nextDouble();
        System.out.println("Cenata na produkta na drebno zadadeno kato: " + cenaD[i]);
    }

}
public static void calcPechalba(){
    for (int i = 1; i < col.length; i++)
        System.out.println("KUR");
}


public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.println("Vavedete broi na stokite prisustvashti v sklada...");
    int m = sc.nextInt();
    System.out.println("Vie izbrahte " + m + " za kolichestvo na stokite.");

    int col[] = new int[m+1];
    double cenaE[] = new double[m+1];
    double cenaD[] = new double[m+1];
    vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1]); // I GET AN ERROR HERE SAYING The method vavediMasiv(int[], double[], double[]) in the type Sclad is not applicable for the arguments (int, double, double)
}
}

I have commented the problematic part. 我已经评论了有问题的部分。 I don't get why doesn't it accept as parameters. 我不明白为什么它不接受作为参数。 I have tried everything but it still won't accept the arrays. 我已经尝试了所有方法,但仍然无法接受数组。 What can I do to fix this up or at least to compromise it and do it another way? 我可以做些什么来解决此问题,或者至少可以妥协并以其他方式解决?

You're passing it int and double values. 您正在传递它的int和double值。 To pass it the int and double arrays you need to do this: 要将其传递给int和double数组,您需要执行以下操作:

vavediMasiv(col, cenaE, cenaD);

For example, here you're passing it the m+1 element of the cenaD array, and not the array itself. 例如,在这里向其传递cenaD数组的m + 1元素,而不是数组本身。

cenaD[m+1]

See... You have defined 看...您已定义

public static void vavediMasiv(int col[], double cenaE[], double cenaD[]) // expecting and accepting whole arrays as arguments.

and you are using : 而您正在使用:

 vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1]); // you are passing individual values of arrays instead of whole array. 

You have to use : 您必须使用:

vavediMasiv(col, cenaE, cenaD); 

你应该通过整个对象

vavediMasiv(col, cenaE, cenaD);

You're passing values in the array and not the actual array. 您在数组中而不是实际数组中传递值。 That's why you are getting the error int cant be converted to int[] because col[m +1] is an int value in the col array. 这就是为什么您将错误int cant be converted to int[]原因,因为col[m +1]col数组中的int值。 Same with the other two arrays. 与其他两个数组相同。

vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1])

all the above are values in the array. 所有上述都在数组的值。 You want 你要

vavediMasiv(col, cenaE, cenaD); 

You also might want to pass an array to this method also, to get rid of this error 您可能还希望将数组传递给此方法,以消除此错误

public static void calcPechalba(int[] col){    <------
    for (int i = 1; i < col.length; i++)
        System.out.println("KUR");
}

You are trying to pass an ( int double double ) parameters to a method which signature says it accepts arrays of ( int double double ). 您正在尝试将( int double double )参数传递给签名表示该方法接受( int double double )数组的方法。

Looking to your source code, you merely need to change the 查看您的源代码,您只需要更改

  vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1]);

call to: 拨电至:

  vavediMasiv(col, cenaE, cenaD);

You should also note that the below method won't compile since there is no local variable col : 您还应注意,由于没有局部变量col ,因此以下方法将无法编译:

public static void calcPechalba(){
   for (int i = 1; i < col.length; i++)
    System.out.println("KUR");
   }

Sorry but I can't understand your methods logical flow to give a better shot. 抱歉,我无法理解您的方法的逻辑流程以提供更好的帮助。

BR. BR。

Your method vavediMasiv(int col[], double cenaE[], double cenaD[]) is expecting three arguments of type int array, double array and double array. 您的方法vavediMasiv(int col [],double cenaE [],double cenaD [])期望使用int array,double array和double array类型的三个参数。 While you are passing int, double and double as arguments. 在传递int时,将double和double用作参数。 That's why it is giving error. 这就是为什么它会出错。

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

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