Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 我的程序从哪里提取随机数?_Java_Arrays_Loops - Fatal编程技术网

Java 我的程序从哪里提取随机数?

Java 我的程序从哪里提取随机数?,java,arrays,loops,Java,Arrays,Loops,我正在codingame.com上进行一项挑战,寻找最接近零的正整数或负整数。我写的代码可以应对六分之四的挑战。另外两个,我得到的输出完全出乎意料,在输入之外 import java.util.*; import java.io.*; import java.math.*; /** * Auto-generated code below aims at helping you parse * the standard input according to the problem state

我正在codingame.com上进行一项挑战,寻找最接近零的正整数或负整数。我写的代码可以应对六分之四的挑战。另外两个,我得到的输出完全出乎意料,在输入之外

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Solution {

    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(); // the number of temperatures to analyse
        //Variables declared here below
        int[] temps =  new int[n];
        int closest = 0;
        // DO I NEED THIS NEXT BLOCK??!!
        //if (in.hasNextLine()) 
        //{
        //    in.nextLine();
        //}
        //For loop here, to take the input from in.nextInt()
        for (int i = 0; i < n; i++)
        {
           //int closest = temps[0];
           temps[i] = in.nextInt();
           closest = Math.abs(temps[0]);
           //System.err.println(temps[i]);
           if (closest > 0)
           {
            if (closest > i)
            {
               closest = i;
            }

           }
           else if (closest < 0)
           {
             if (closest < i)
             {
                 closest = i;
             }
           }
        }

        //String temps = in.nextLine(); // the n temperatures expressed as integers ranging from -273 to 5526

        // Write an action using System.out.println()
        // To debug: System.err.println("Debug messages...");
        System.err.println("Final Results: n = " + n);
        System.err.println("Temps are: ");
        for (int i = 0; i < n; i++)
        {
            System.err.println(temps[i]);
        }
        System.err.println("Closest to zero is " + closest);
        System.out.println(closest);
    }
}

我不认为你的for循环在做你想让它做的事情。一点也不

temps[i] = in.nextInt();  // This is sensible enough
closest = Math.abs(temps[0]); // Every time your loop runs, the closest value to 0 is... the absolute value of the first int in the array?
//System.err.println(temps[i]);
if (closest > 0)  // This is only false if closest is 0
    {
        if (closest > i) // This makes the outer if redundant, since i >= 0. It's also not clear what the point of this condition even is
        {
           closest = i; // Here's the big one: you're setting closest to your iterator, the index of the current number in the array instead of the number itself. 
        }
     }
       else if (closest < 0) // This is never true, since absolute values are always >=0
       {
         if (closest < i)
         {
             closest = i;
         }
       }
    }
检查代码,一次一行,跟踪每个变量的值,你将能够找到你得到的数字来自哪里,以及你所做的是否有意义


看起来你在那里的某个地方找到了大部分正确的部件,但是它们没有正确地组合在一起。当你修改算法时,不要忘记你的目标:你想在列表中找到绝对值最小的数字。您可以通过检查下一个数字的绝对值是否小于当前最小值来完成此操作。

您调试过吗?而且你没有包括足够的信息。你甚至还没有说你期望它产生什么。预期结果是-5吗?问题不清楚。如果您设置了nexist=Math.abs。。。;,如果最近值<0,您如何期望其他值为真?你知道该怎么做,对吧?谢谢大家的反馈,我会把这篇文章拉出来。