Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果输出为0,如何打印数组?_Java_Arrays - Fatal编程技术网

Java 如果输出为0,如何打印数组?

Java 如果输出为0,如何打印数组?,java,arrays,Java,Arrays,我解决了这个问题,效果很好,但有一个小问题,如果光线只包含一个数字或相同的数字,它会返回0,但我想要那个数字。我的代码: public class RaySmallest { public static int go(int[] ray) { int smallest = 0; for(int i =0;i<ray.length;i++) { if(smallest > ray[i])

我解决了这个问题,效果很好,但有一个小问题,如果光线只包含一个数字或相同的数字,它会返回0,但我想要那个数字。我的代码:

public class RaySmallest
{
    public static int go(int[] ray)
    {
          int smallest = 0;
      for(int i =0;i<ray.length;i++) 
      {
            if(smallest > ray[i]) 
            {
                smallest = ray[i];
            }
      } 
      return smallest;
 }
}
就像第四行一样,我的代码给我0,但我需要输出为32767

我需要这些答案

-99
-99
-11818
32767
255
-555
10
-111
-989
12
-455
-5000

谢谢

如果最小的数字大于零,则该数字实际上没有设置,这就是为什么如果只有一个数字(可能低于零),则无法获得最小的数字

public static int go(int[] ray) {
    int smallest = ray.length > 0 ? ray[0] : -1;
    for (int value : ray) {
        if (smallest > value) {
            smallest = value;
        }
    }
    return smallest;
}

好的,这是您代码的一部分:

 int smallest = 0;
  for(int i =0;i<ray.length;i++) 
  {
        if(smallest > ray[i]) 
        {
            smallest = ray[i];
        }
  } 
  return smallest;
基本上这告诉我,最小的是0。比较0>光线[i],第4行是32767。0不大于32767,因此最小值仍然等于0并返回为0

int smallest = 0;
int[] numArray = new int[ray.length];
 for(int i=0;i<ray.length;i++)
 { 
       if(ray.length == 1)
             return ray[i];
       if(smallest > ray[i])
             smallest = ray[i];
       if(ray[i] == ray[ray.length - 1] && i-ray.length > 0 && i==ray.length-2){
             int x = 0;
             for(int x=0;x<ray.length-1;x++){
                  if(ray[i] != ray[x])
                    break;
             }
             if(ray[i] == ray[x])
                  return ray[i];
       }

}
return smallest;

此解决方案应注意数组中只有一个值,或者数组中的所有值都相等。

更简单:在检查光线的长度>0后,使用Integer.MAX_值或光线[0]初始化最小值。是的,这实际上更好。有人能解释为什么使用[?光线[0]:-1;]这检查数组是否至少有1个元素长,如果是,则使用firest值进行比较,否则使用-1,如果元素的大小为0,则返回-1。完全没有问题:这不是真正的答案,这只是对问题的更深入描述,而不是解决方案。
int smallest = 0;
int[] numArray = new int[ray.length];
 for(int i=0;i<ray.length;i++)
 { 
       if(ray.length == 1)
             return ray[i];
       if(smallest > ray[i])
             smallest = ray[i];
       if(ray[i] == ray[ray.length - 1] && i-ray.length > 0 && i==ray.length-2){
             int x = 0;
             for(int x=0;x<ray.length-1;x++){
                  if(ray[i] != ray[x])
                    break;
             }
             if(ray[i] == ray[x])
                  return ray[i];
       }

}
return smallest;