【问题标题】:How can I change the values exceeding the int data type to the long data type in this code?如何在此代码中将超过 int 数据类型的值更改为 long 数据类型?
【发布时间】:2016-09-29 04:04:27
【问题描述】:

在这段代码的最后一部分(最后一个 for 循环和 if 语句所在的位置)中,我试图将 integers 的数据类型更改为 long,当它们超过整数数据类型限制时。我在这段代码中做错了什么?当我运行时,我得到的值与我什至尝试将它们更改为 long 之前相同(它们是越来越大的整数,直到它们变为负数)。

public class UniqueElements {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int maxValue = 0;
        int numElements = 0;
        int programRuns = 12;
        Scanner sc = new Scanner(System.in);
        for (int newExecution = 0; newExecution <= programRuns; newExecution++ ) {
            System.out.print("Enter the maximum value for an element: ");
            //prompt user to enter the maximum value
            maxValue = sc.nextInt(); //user input for max value
            System.out.print("Enter the number of elements in the array: ");
            numElements = sc.nextInt(); //number of elements in an array
            int A[] = new int[numElements];
            //array comprising of the number of elements chosen by the user
            int totalComp = 0; //set total comparisons to 0
            for (int runs = 1; runs <= 100; runs++) { //program runs 100 times
                Random rand = new Random(System.nanoTime());
                //initiate the random number generator
                int numComp = 0; //set number of comparisons to 0
                for (int index = 0; index < numElements; index++) { 
                    A[index] = rand.nextInt(maxValue); 
                    //length of array is the number of elements the user puts in
                }
                for (int i = 0; i < A.length; i++) { //for each integer in the array
                    for (int j = i + 1; j < A.length; j++) { 
                        //for each integer following i
                        if (A[i] == A[j]) { //if the are equal to eachother
                            //end the if statement
                            break;
                        }
                        if (numComp == (int)numComp) {
                           numComp++; 
                        }
                        else {
                            Long.valueOf(numComp);
                            numComp++;
                        }
                    }
                }
                totalComp+= numComp; 
            } //end 100 loops
            if (totalComp == (int)totalComp) 
                System.out.println("Average number of comparisons: " + totalComp / 100);
            else {
                System.out.println("Average number of comparisons: " + 
                                    Long.valueOf(totalComp) / 100L);
            }

            }
        }
    }

【问题讨论】:

    标签: java int long-integer


    【解决方案1】:

    更新:

    不知何故....将总运行次数更改为 2 并将 totalComp 变量除以 2 和 2L 使其工作。有谁知道这是怎么改变的?

    【讨论】:

      【解决方案2】:

      在声明变量后,您无法更改其类型。您可以将该变量转换为另一种类型,或将其“解释”为另一种类型(如在您的 Long.valueOf() 中),但该变量仍然保留您声明的任何类型。

      在您的代码中,您已将totalComp 声明为int,这在Java 中意味着它包含32 位(其中一个是符号位)。没有办法让 Java 在 int 中存储超过 32 位。如果您继续添加超出Integer.MAX_VALUE,或减去低于Integer.MIN_VALUE,变量中的值将简单地低于/溢出。所以你的 for 循环之后的这个语句没有做你所期望的,并且永远是正确的:if (totalComp == (int)totalComp)

      换句话说,您不能“回到过去”并重新将原始 int 声明为 long,因为您在运行时发现需要存储更大的值。解决此问题的最简单方法是将totalComp 声明为long。如果由于某种原因无法更改totalComp 的类型,则可以在执行计算之前检测上溢/下溢;详情请见this answer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-22
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        • 2023-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多