简体   繁体   English

我想在我的android应用程序中使用大整数,但是每当我应用运算符时,我都会出错?

[英]I want to use big integer in my android app but when ever i apply operators i get error?

I want to calculate factorial my code at present is this: 我现在要计算阶乘我的代码是这样的:

else if(btn.getText().equals("x!"))
    {
        double LeftVal = Double.parseDouble(currentInput);
        double result = Double.NaN;
        if(currentInputLen > 0 && LeftVal >0)
        {
            result = Factorial(LeftVal);
        }

        resultText.setText(result+"");
    }

code to calculate factorial is this: 计算阶乘的代码是这样的:

private double Factorial(double input)
{
    double output=input;
    for(int i=(int) (input-1);i>0;i--)
    {
        output*=i;
    }

    return output;
}

I imported java.math.*; 我导入了java.math。*; and changed my code to this: 并将我的代码更改为此:

else if(btn.getText().equals("x!"))
    {
        BigInteger LeftVal = BigInteger(currentInput);
        BigInteger result = 0;
        if(currentInputLen > 0 && LeftVal >0)
        {
            result = Factorial(LeftVal);
        }

        resultText.setText(result+"");
    }

and factorial method to this 和阶乘方法

private BigInteger Factorial(BigInteger input)
{
    BIgInteger output=input;
    for(int i=(int) (input-1);i>0;i--)
    {
        output*=i;
    }

    return output;
}

I saw on android developer site that BigInteger(string) will convert that string to big integer but that does not work and shows error also there were errors on using regular mathematical errors. 我在android开发人员网站上看到BigInteger(string)会将字符串转换为大整数,但这不起作用并显示错误,并且在使用常规数学错误时也会出现错误。

The reason i want to use big integer is because if i calculate 12! 我要使用大整数的原因是因为如果我计算12! using double it shows answer in scientific form with e raised to power something i want exact answer. 使用double它以科学的形式显示答案,并提出e以增强我想要的确切答案。

For your factorial method using BigInteger you could use: 对于使用BigInteger的析因方法,可以使用:

private BigInteger factorial(BigInteger input) {
    BigInteger output = input;
    for (BigInteger i = input.subtract(BigInteger.ONE); i.compareTo(BigInteger.ZERO) > 0; i = i.subtract(BigInteger.ONE)) {
        output = output.multiply(i);
    }
    return output;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在单击任务时添加子任务,但是当我单击任务时出现以下错误 - I want to add sub tasks on clicking the task but I get the following error when ever I click the task 每当我运行tomcat时,我都会收到NoSuchBeanCreation错误 - When ever i run tomcat , i get NoSuchBeanCreation error 安装我的应用程序时,我会收到一个随机的Notification Android - When I install my app I get a random Notification Android Android Studio应用更改询问我是否要重新启动应用 - Android Studio Apply Changes asks if I want to Restart App 因为当我在 Android 应用程序中使用 Firebase Firestore 的依赖项时出现错误? - Because when I use the dependency of Firebase Firestore in my Android application I get an error? 为什么我的查询无法正常运行? 每当我通过executeQuery方法传递字符串变量时,都会出现错误 - Why won't my query run properly? When ever I pass a string variable through the executeQuery method I get an error 为什么我的服务不适用于Android? (我只想记录5秒钟的东西) - Why doesn't my Service work in Android? (I just want to log something ever 5 seconds) 我希望我的程序在用户输入除整数以外的任何内容并将其循环回时显示错误消息 - I want my program to display an error message when the user inputs anything other than an integer and loop it back 我什么时候需要使用@WebServiceRef? - When will I ever need to use @WebServiceRef? 每当我将插页式 ID 更改为 Admob 上的 ID 并尝试在我的手机上测试该应用程序时 - when ever i change interstitial ID to the one on Admob and i try to test the app on my phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM