Java return (值) 错误
Java return (value) error
我在我的代码中尝试了不同的东西,但我总是会出错。
该程序的说明是我需要一个函数(除了 main 之外)接收整数值数组的参数和第二个参数告诉用户在数组中查找的期望值。
它还必须 return 期望值在数组上重复了多少次。
错误与计数器有关,也有一些在主函数中。
我想我没有正确 returning 计数器值。
这是我当前的代码:
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");
}
}
}
你让你的方法接受三个参数。
static int findRepetition(int listOfValues[], int targetValue, int counter)
问问自己——你想要"pass in a counter",还是只return
呢?说明书说的是后者。
当您调用此方法时,您没有提供正确的输入。
findRepetition(targetValue, counter);
listOfValues
呢?您想 findReptition
在 listOfValues
上 targetValue
,对吗?因此,为该方法调用提供正确的参数。
同样,您可能不需要传入的 counter
。因为Java is always pass-by-value
相反,您想要 return counter
,正如您所写的那样。你在找这个。
int counter = findRepetition(listOfValues, targetValue);
修复代码的其余部分是一项学习练习。
您没有使用必需的参数调用 findRepetition(),findRepetition 方法有 3 个参数,但您只传递了 2 个参数。
您的代码中存在多个问题。
public static int main(String[] args)
应该是 public static
void main(String[] args)
findRepetition
接受三个参数,
但是你传递了两个参数
counter
变量未声明
逻辑错误,如果计数器值小于 listOfValues,while((counter < listOfValues.length))
将继续执行。
static int findRepetition(int listOfValues[], int targetValue) {
int i;
int counter = 0;
for (i = 0; i < listOfValues.length; i++) {
if (listOfValues[i] == targetValue) {
counter = counter + 1;
}
}
return counter;
}
public static void 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();
int counter = findRepetition(listOfValues, targetValue);
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");
}
}
我在我的代码中尝试了不同的东西,但我总是会出错。
该程序的说明是我需要一个函数(除了 main 之外)接收整数值数组的参数和第二个参数告诉用户在数组中查找的期望值。
它还必须 return 期望值在数组上重复了多少次。
错误与计数器有关,也有一些在主函数中。
我想我没有正确 returning 计数器值。
这是我当前的代码:
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");
}
}
}
你让你的方法接受三个参数。
static int findRepetition(int listOfValues[], int targetValue, int counter)
问问自己——你想要"pass in a counter",还是只return
呢?说明书说的是后者。
当您调用此方法时,您没有提供正确的输入。
findRepetition(targetValue, counter);
listOfValues
呢?您想 findReptition
在 listOfValues
上 targetValue
,对吗?因此,为该方法调用提供正确的参数。
同样,您可能不需要传入的 counter
。因为Java is always pass-by-value
相反,您想要 return counter
,正如您所写的那样。你在找这个。
int counter = findRepetition(listOfValues, targetValue);
修复代码的其余部分是一项学习练习。
您没有使用必需的参数调用 findRepetition(),findRepetition 方法有 3 个参数,但您只传递了 2 个参数。
您的代码中存在多个问题。
public static int main(String[] args)
应该是public static void main(String[] args)
findRepetition
接受三个参数, 但是你传递了两个参数counter
变量未声明逻辑错误,如果计数器值小于 listOfValues,
while((counter < listOfValues.length))
将继续执行。static int findRepetition(int listOfValues[], int targetValue) { int i; int counter = 0; for (i = 0; i < listOfValues.length; i++) { if (listOfValues[i] == targetValue) { counter = counter + 1; } } return counter; } public static void 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(); int counter = findRepetition(listOfValues, targetValue); 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"); } }