【问题标题】:Java return (value) errorJava 返回(值)错误
【发布时间】: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


【解决方案1】:

您的代码中存在多个问题。

  1. public static int main(String[] args) 应该是 public static void main(String[] args)
  2. findRepetition 接受三个参数, 但是你传递了两个 agruments
  3. counter 变量未声明
  4. 逻辑缺陷,如果计数器值小于 listOfValues,while((counter &lt; 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");
        }
    
    }
    

【讨论】:

  • 它终于运行了。我会继续研究数组,如何返回等,这样我就可以每天多一点理解。
【解决方案2】:

你让你的方法接受三个参数。

static int findRepetition(int listOfValues[], int targetValue, int counter)

问问你自己——你是想“通过柜台”,还是只用return呢?说明书说后者。


当您调用此方法时,您没有提供正确的输入。

findRepetition(targetValue, counter);

listOfValues 呢?你想findReptitionlistOfValues 上为targetValue,对吗?因此,在该方法调用中提供正确的参数。

同样,您可能不需要 counter 传入。因为Java is always pass-by-value


相反,您想return counter,正如您所写的那样。你正在寻找这个。

int counter = findRepetition(listOfValues, targetValue);

修复代码的其余部分是一项学习练习。

【讨论】:

    【解决方案3】:

    您没有使用必需的参数调用 findRepetition(),findRepetition 方法需要 3 个参数,但您只传递了 2 个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-29
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 2015-06-19
      相关资源
      最近更新 更多