如何向现有程序添加长整数

发布于 2024-12-08 06:48:56 字数 877 浏览 0 评论 0原文

该代码有效。但是,我需要包含长整数。我怎样才能做到这一点?我已经尝试了一百万件事。我也不擅长这个,所以我花了 5 倍的时间才得到一个简单的代码。请帮忙。

import java.util.Scanner;
public class Exercise2_6M
{
    public static void main(String[] args)
    {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
        // Enter amount
        System.out.print("Enter an integer:");
        int integer = input.nextInt();
        // Calculations
        int rinteger = Math. abs (integer);
        int sum = 0;
        int i=0;
        while(rinteger / Math.pow(10,i) > 0)
        {
            sum+=getDigit(rinteger,i);
            i++;
        }
        // Display results
        System.out.println("Sum all digits in " + integer + " is " + sum);
    }
    public static int getDigit(int num, int power)
    {
        return (num % (int)Math.pow(10,power+1)) / (int)Math.pow(10,power);
    }
}

The code works. But, I need to include long integers. How can I do that? I've tried a million things. I'm not good at this either so it takes me 5 times longer to get a simple code. Please help.

import java.util.Scanner;
public class Exercise2_6M
{
    public static void main(String[] args)
    {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
        // Enter amount
        System.out.print("Enter an integer:");
        int integer = input.nextInt();
        // Calculations
        int rinteger = Math. abs (integer);
        int sum = 0;
        int i=0;
        while(rinteger / Math.pow(10,i) > 0)
        {
            sum+=getDigit(rinteger,i);
            i++;
        }
        // Display results
        System.out.println("Sum all digits in " + integer + " is " + sum);
    }
    public static int getDigit(int num, int power)
    {
        return (num % (int)Math.pow(10,power+1)) / (int)Math.pow(10,power);
    }
}

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

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

发布评论

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

评论(2

九歌凝 2024-12-15 06:48:56

将输入值作为字符串读取,然后使用 BigInteger 类执行非常大的值的计算。

Read the input value as a string and then use the BigInteger class to perform calculations with very large values​​.

哭泣的笑容 2024-12-15 06:48:56

递归解决方案可以更精简:

import java.util.Scanner;
public class Exercise2_6M
 {
    public static void main (String [] args)
    {
        Scanner input = new Scanner (System.in);
        System.out.print ("Enter an long:");
        long lng = input.nextLong ();
        int sum = getDigitSum (lng);
        System.out.println ("Sum all digits in " + lng + " is " + sum);
    }

    public static int getDigitSum (long num)
    {
        if (num < 10L) return (int) num;
        else return ((int)(num % 10)) + getDigitSum (num/10L);
    }
}

A recursive solution can be much leaner:

import java.util.Scanner;
public class Exercise2_6M
 {
    public static void main (String [] args)
    {
        Scanner input = new Scanner (System.in);
        System.out.print ("Enter an long:");
        long lng = input.nextLong ();
        int sum = getDigitSum (lng);
        System.out.println ("Sum all digits in " + lng + " is " + sum);
    }

    public static int getDigitSum (long num)
    {
        if (num < 10L) return (int) num;
        else return ((int)(num % 10)) + getDigitSum (num/10L);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文