Java新手,类y中的方法X无法应用
我是java新手,正在寻求一些建议。我被分配了下面的问题,我无法让比较方法在我的生命中运行。它不会编译。我收到以下错误:Java新手,类y中的方法X无法应用,java,Java,我是java新手,正在寻求一些建议。我被分配了下面的问题,我无法让比较方法在我的生命中运行。它不会编译。我收到以下错误: error: method compare in class Plateau cannot be applied to given types; compare(a[N]); r
error: method compare in class Plateau cannot be applied to given types;
compare(a[N]);
required: int[],int
found: int
reason: actual and formal argument lists differ in length
任何帮助都将不胜感激
1.4.21最长高原。给定一个整数数组,求最长连续等值序列的长度和位置,其中该序列前后的元素值较小。数组应该被传递给一个方法,结果应该被打印到屏幕上
public class Plateau{
public static void main(String[] args) {
int N = args.length;
int[] a = new int [N];
for (int i=0; i < N; i++){
int number = Integer.parseInt(args[i]);
a[i]=number;
}
compare(a[N]);
}
public static void compare(int[] a, int N){
int comp = a[0];
int current_length=0;
int max=0;
int maxlength=0;
for(int l=0; l < N; l++){
if (a[l] > comp){
current_length = 0;
comp = a[l];
max = a[l];
}
if (a[l] == comp){
current_length+=1;
comp = a[l];
}
else if (a[l] < comp && a[l] < max){
comp = a[l-1];
current_length=maxlength;
l++;
}
}
System.out.println(max);
System.out.println(maxlength);
}
}
公共类平台{
公共静态void main(字符串[]args){
int N=args.length;
int[]a=新的int[N];
对于(int i=0;icomp){
当前_长度=0;
comp=a[l];
最大值=a[l];
}
如果(a[l]==comp){
当前_长度+=1;
comp=a[l];
}
否则如果(a[l]
很明显:参数需要一个数组和一个值(length?index),但您只是从数组中传递一个值
转身
compare(a[N]);
到
您的方法签名与您尝试调用它的方式不匹配。在main方法中,您试图调用一个名为compare的方法,该方法接受一个整数数组,但您仅有的定义是一个compare方法,该方法同时接受一个整数数组和一个整数。用法和定义必须一致,否则编译器将不知道您要做什么。您的方法
public static void compare(int[] a, int N){
接受两个参数,一个是整数数组,另一个是整数
在main方法中调用该方法时,只传递一个参数
public static void main(String[] args) {
int N = args.length;
int[] a = new int [N];
for (int i=0; i < N; i++){
int number = Integer.parseInt(args[i]);
a[i]=number;}
compare(a,N); // pass a integer along with your integer array (you have to use your array variable which is a and not a[N])
}
您已经在这里声明了数组,因此需要传递变量a而不是[N]问题在于参数和方法签名的问题。正如我所看到的,你正在学习,我不会给你一个完整的解决方案。我只会给你指出一个解决办法
compare
需要两个参数int[]a,int N
,但您仅使用一个compare(a[N])
a[N]
是错误的,因为它将索引数组外的元素(请注意数组索引从0到N-1)a
是int[]类型的数组,因此需要将其用作调用compare
N
是数组中的元素数(int类型),因此这可能是第二个参数尝试
比较(a,N)
compare(int[]a,int N)
接受2个参数,但使用1个参数调用compare(a[N])
。从任何初学者的书开始(语言不重要)谢谢!有一次我声明了一个[],我没有意识到我可以将它作为一个简单的
public static void main(String[] args) {
int N = args.length;
int[] a = new int [N];
for (int i=0; i < N; i++){
int number = Integer.parseInt(args[i]);
a[i]=number;}
compare(a,N); // pass a integer along with your integer array (you have to use your array variable which is a and not a[N])
}
int[] a = new int [N];