如何在此代码中将超过int数据类型的值更改为long数据类型?

时间:2016-09-29 04:04:27

标签: java int long-integer

在此代码的最后部分(最后一个for循环和if语句),我试图在buffer.write超过整数数据时将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);
            }

            }
        }
    }

2 个答案:

答案 0 :(得分:0)

更新

以某种方式......将总运行次数更改为2并将totalComp变量除以2和2L使其工作。有谁知道这改变了什么?

答案 1 :(得分:0)

在声明已声明后,您无法更改变量的类型。您可以将该变量强制转换为另一种类型,或将其“解释”为另一种类型(如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