【发布时间】:2016-11-26 05:07:17
【问题描述】:
我在我的代码中尝试了不同的东西,但总是出错。
程序的说明是我需要一个函数(除了 main 函数),它接收整数值数组的参数和第二个参数,告诉用户在数组中查找的期望值。
它还必须返回期望值在数组上重复的次数。
这些错误与计数器有关,也有一些在 main 函数中。
我想我没有正确返回计数器值。
这是我当前的代码:
import java.util.Scanner;
import java.util.Arrays;
public class ArregloBusqueda2
{
static int findRepetition(int listOfValues[], int targetValue, int counter)
{
int i;
boolean found = false;
for(i=0; i<listOfValues.length; i++)
{
while((counter < listOfValues.length))
{
if(listOfValues[i] == targetValue)
{
counter = counter + 1;
}
}
}
return counter;
}
public static int main(String[] args)
{
Scanner input = new Scanner (System.in);
int targetValue;
int listOfValues[] = {1,6,3,8,5,8,3,4,8,3};
System.out.println("Please enter the desire number to look for: ");
targetValue=input.nextInt();
findRepetition(targetValue, counter);
if(counter != 0)
{
System.out.println("The frequency of the number " + targetValue + " is: " + counter);
}
else
{
System.out.println ("The number " + targetValue + " is not contained in the array list");
}
}
}
【问题讨论】:
-
findRepetition返回一个值,但你没有捕获它。 -
另外,错误是“找不到符号”?
findRepetition(targetValue, counter);是如何知道counter的? -
是的,错误是找不到符号。
标签: java arrays return counter