简体   繁体   中英

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. To pass it the int and double arrays you need to do this:

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]

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. 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 ).

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 :

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.

Your method vavediMasiv(int col[], double cenaE[], double cenaD[]) is expecting three arguments of type int array, double array and double array. While you are passing int, double and double as arguments. That's why it is giving error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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