簡體   English   中英

如何防止長時間溢出?

[英]How do I prevent a long overflow?

我的作業要求我們要求用戶輸入一個數字並打印它的階乘,

它還要求我們不允許用戶選擇任何負數或超過 25 的數字,當他們選擇時,我們將他們循環回掃描儀以重新租用一個數字

我們還被告知要將數字存儲在 long 中,但是當我這樣做時,如果用戶輸入超過 20 且 =<25 的數字,答案將變為否定,從而導致 long 溢出。

我嘗試將 long 更改為 BigInteger,但是當我在每一行上不斷收到錯誤時,我使用 BigInteger 而不是 long

boolean correctInputn= false;
    
    while(!correctInputn)
    {
    long number;// declares variables for storing number
    long factorial = 1;// declare variable for storing factorial
    

    System.out.println("Enter a number between 1 and 25"); // tells user to enter number
    number = scanner.nextLong();
    
         if (number <0)  
            {System.out.println("Positive numbers only");// if number entered is negative
             correctInputn = false; continue;} // if user enters number less than 0 loops back to code start
       
       
         else if (number > 25)
             { System.out.println("Number to large to print");
               correctInputn = false; continue;} // if user enters number over 25 loops back to code start
        
    
          else  {
      // if user enter 10, counter starts at 10 and runs to two
        for(long mynumber = number; mynumber >= 1; mynumber--) {
            factorial = factorial*mynumber; // mynumber would contain different values and that is multiplied by value present in factorial value and stored again in factorial variable
        }
    
    System.out.println("The factorial of " + number +" is equal to " + factorial);
    break;
    }
    }

BigInteger是一種引用類型 - 類似於 java 中的所有類型,除了longintdoubleshortfloatcharbyteboolean的硬編碼列表。 您可以在其上調用方法。 如果它左邊的類型是一個String ,那么可以使用+ ,它將調用它們的toString()並將它們連接起來。 就是這樣。 它們不“支持”-、+、* 等。 相反,他們有方法.multiply.add和朋友。 請注意,這不會將數字添加到 object,它會計算值並返回它,例如,您會編寫factorial = factorial.multiply(myNumber) 您必須用適當的方法替換所有運算符,並且必須轉換BigInteger對象中的所有數字,例如使用BigInteger.valueOf(20)

您無法神奇地阻止多頭溢出。 這是 64 位數字數學的一個基本方面:由於基本的數學原因,您不能在 64 位中存儲超過 2^64 個不同的東西,原因與 1+1=2 相同。

**I FOUND A SOLUTION AND NOW IT DOESNT OVERFLOW**
**MAIN**
    import java.util.Scanner;
    public class Main {
    public static void main(String[] args) {
        Factorial factNum;
        Scanner in = new Scanner(System.in);
        boolean flagCheck = false;

        System.out.println("Please give a number between 0 - 25: ");
        factNum = new Factorial(in.nextLong());

        //check if the number is in the limits
        while(!flagCheck) {

            if (factNum.getVarForFact() < 0){
                System.out.println("Number must be over 0. Please enter again a 
                number between 0-25");
                factNum = new Factorial(in.nextLong());
            } else if (factNum.getVarForFact() > 25){
                System.out.println("Number must be under 25. Please enter again a 
                number between 0-25");
                factNum = new Factorial(in.nextLong());
            } else if (factNum.getVarForFact() == 0) {
                factNum.setFinalFact(1);
                System.out.println("The factorial of " + factNum.getVarForFact() + " 
                is " + factNum.getFinalFact());
                flagCheck = true;
            } else{
                factNum.factorial();
                System.out.println("The factorial of " + factNum.getVarForFact() + " 
                 is " + factNum.getFactorial());
                flagCheck = true;
            }
        }

    }
}

階乘 CLASS

    import java.math.BigInteger;

    class Factorial {
    //variable for storing the given number and find the factorial
    private long varForFact;
    private long finalFact;

    private BigInteger factorial = BigInteger.ONE;
    //Constructor
    Factorial(long varForFact){
        this.varForFact = varForFact;
    }

    //Getter
    public long getVarForFact(){
        return this.varForFact;
    }

    public long getFinalFact() {
        return finalFact;
    }

    public BigInteger getFactorial() {
        return factorial;
    }

    //Setter
    public void setVarForFact(long varForFact){
         this.varForFact = varForFact;
    }


    public void setFinalFact(long finalFact) {
        this.finalFact = finalFact;
    }

    //factorial method, it will return the factorial
    public void factorial(){

        for (long i = this.varForFact; i > 0; i--) {
            this.factorial = this.factorial.multiply(BigInteger.valueOf(i));
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM