给定双精度数组,如何打印最接近零的整数?

发布于 2025-01-10 10:57:55 字数 1017 浏览 0 评论 0原文

我被这个家庭作业问题困住了:

Print out the double that is closest to 0, positive or negative.

我当前的代码是:

import java.util.Arrays;
import java.util.Scanner;

public class Code {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        // Read input for the test cases
        int N = scanner.nextInt();
        double[] doubles = new double[N];
        double closestToZero = N;
        for(int i = 0; i < N; i++) {
            doubles[i] = scanner.nextDouble();
        }

        // Your code here
        for(int a = 0; a < N; a++) {
            if (Math.abs(a) < 0) {
                closestToZero = a;
            }

                else if(Math.abs(a) == 0.0) {
                    closestToZero = a;
                }
                else {

                }
            }         
        System.out.println("The double closest to 0 is " + closestToZero);

    }
}

问题:代码不断打印出 0.0,即使它是错误的答案,错误的原因是什么?

I am stuck on this homework problem:

Print out the double that is closest to 0, positive or negative.

My current code is:

import java.util.Arrays;
import java.util.Scanner;

public class Code {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        // Read input for the test cases
        int N = scanner.nextInt();
        double[] doubles = new double[N];
        double closestToZero = N;
        for(int i = 0; i < N; i++) {
            doubles[i] = scanner.nextDouble();
        }

        // Your code here
        for(int a = 0; a < N; a++) {
            if (Math.abs(a) < 0) {
                closestToZero = a;
            }

                else if(Math.abs(a) == 0.0) {
                    closestToZero = a;
                }
                else {

                }
            }         
        System.out.println("The double closest to 0 is " + closestToZero);

    }
}

Issue: The code keeps printing out 0.0 even though it is the incorrect answer, What would be the cause for the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2025-01-17 10:57:55

最接近0的是绝对值最小的数。那么你只需要找到最小值即可。如果找到较小的数字,则更新closestToZero。

import java.util.Arrays;
import java.util.Scanner;

public class Code {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        // Read input for the test cases
        int N = scanner.nextInt();
        double[] doubles = new double[N];
        double closestToZero = Double.MAX_VALUE;
        for(int i = 0; i < N; i++) {
            doubles[i] = scanner.nextDouble();
        }

        // Your code here
        for(int a = 0; a < N; a++) {
            if (Math.abs(doubles[a]) < Math.abs(closestToZero)) {
                closestToZero = doubles[a];
            }
        }   
        
        System.out.println("The double closest to 0 is " + closestToZero );
    }
}

Closest to 0 is the smallest number in absolute value. Then you only have to find the minimum. Update closestToZero if a smaller number is found.

import java.util.Arrays;
import java.util.Scanner;

public class Code {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        // Read input for the test cases
        int N = scanner.nextInt();
        double[] doubles = new double[N];
        double closestToZero = Double.MAX_VALUE;
        for(int i = 0; i < N; i++) {
            doubles[i] = scanner.nextDouble();
        }

        // Your code here
        for(int a = 0; a < N; a++) {
            if (Math.abs(doubles[a]) < Math.abs(closestToZero)) {
                closestToZero = doubles[a];
            }
        }   
        
        System.out.println("The double closest to 0 is " + closestToZero );
    }
}
对不⑦ 2025-01-17 10:57:55

这是从 Double[] 数组中查找最接近零的值的更快、更有效的方法。

import java.util.Arrays;
import java.util.Scanner;

public class StackOverflow_Tester {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of integers: ");
        int N = in.nextInt();
        double[] doubles = new double[N];
        for(int i = 0; i < N; i++) {
            doubles[i] = in.nextDouble();
        }
        double curr = 0;
        double near = doubles[0];
        // find the element nearest to zero
        for ( int i=0; i < doubles.length; i++ ){
            curr = doubles[i] * doubles[i];
            if ( curr <= (near * near) )  {
                near = doubles[i];
            }
        }
        System.out.println( near );
    }
}

here is a faster and more efficient way of finding the closent value to zero from a Double[] array.

import java.util.Arrays;
import java.util.Scanner;

public class StackOverflow_Tester {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of integers: ");
        int N = in.nextInt();
        double[] doubles = new double[N];
        for(int i = 0; i < N; i++) {
            doubles[i] = in.nextDouble();
        }
        double curr = 0;
        double near = doubles[0];
        // find the element nearest to zero
        for ( int i=0; i < doubles.length; i++ ){
            curr = doubles[i] * doubles[i];
            if ( curr <= (near * near) )  {
                near = doubles[i];
            }
        }
        System.out.println( near );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文