数组中的最小值

时间:2017-11-20 00:27:46

标签: java

我必须在数组中找到最小的值并打印它,我有代码但是我无法将它与我的跑步者文件连接,它告诉我RaySmallest中的“go(int [])无法应用于( int,int,int,int,int,int,int,int,int,int,int,int)“对于我的所有12个样本。

public class RaySmallest
{
 public static int go(int[] ray)
 {
   {
        int[] numbers = {};
     int smallest = Integer.MAX_VALUE;
        for(int i =0;i<numbers.length;i++) 
        {
        if(smallest > numbers[i]) 
        {
            smallest = numbers[i];
        }
        }
    System.out.println("Smallest number in array is : " +smallest);
    }
  return 0;
 }
}

这是我的跑步者档案

public class SmallestRunner
{
 public static void main( String args[] )
 {
  RaySmallest.go(-88,1,2,3,4,5,6,7,8,90,10,5);
  System.out.println();

  RaySmallest.go(10,9,8,7,6,5,4,3,2,1,-99); 
  System.out.println();

  RaySmallest.go(10,20,30,40,50,-11818,40,30,20,10);
  System.out.println();

  RaySmallest.go(65478);
  System.out.println();

  RaySmallest.go(578,578);
  System.out.println();

  RaySmallest.go(6,13,-98,100,-987,7);
  System.out.println();

  RaySmallest.go(9,9,9,13,567);
  System.out.println();

  RaySmallest.go(-222,1,5,6,9,12,29,1);
  System.out.println();

  RaySmallest.go(9,8,7,6,5,4,3,2,0,-2,6);
  System.out.println();

  RaySmallest.go(12,15,18,21,23,1000);
  System.out.println();

  RaySmallest.go(250,19,17,15,13,11,10,9,6,3,2,1,0);
  System.out.println();

  RaySmallest.go(9,10,-8,10000,-5000,-3000);
  }
 }

1 个答案:

答案 0 :(得分:2)

您的方法需要int[]作为参数:int go(int[] ray)。传递它的简单方法是:

RaySmallest.go(new int[] {6, 13, -98, 100, -987, 7});

或者,您可以定义go方法,使其需要多个参数,如下所示:

int go(int... ray)

现在你可以像以前一样调用它,没有问题:

RaySmallest.go(6, 13, -98, 100, -987, 7);

此外,您的代码中还有另一个错误:使用go()参数的ray方法,而是遍历numbers数组,即空。要快速解决问题,请执行以下操作:

int[] numbers = ray;