Java Integer addition [closed]

拈花ヽ惹草 提交于 2019-12-12 05:08:24

问题


my program has the user enter some 5 digit number, I need a way to take that 5 digit integer and add all the digits together. For example, the user enters 26506, the program does 2+6+5+0+6 and returns 19. I believe this would be done by some sort of loop but am unsure of where to start.

For clarification, this integer could be anything, just has to be 5 digits.


回答1:


From the top of my head, you can convert it to string and iterate over each char, accumulating the value with ( charAt( position ) - '0' ). I'm away from a java compiler right now, but I guess this should work. Just make sure you have numerical data only.




回答2:


int sum = 0;
while(input > 0){
    sum += input % 10;
    input = input / 10;
}



回答3:


Each time you take a modulus of a number by 10, you get digits at ones place. And each time to divide your number by 10, you get all the digits except the ones digit. So you can use this approach to sum all your digits like this: -

22034 % 10 = 4
22034 / 10 = 2203

2203 % 10 = 3
2203 / 10 = 220

220 % 10 = 0
220 / 10 = 22

22 % 10 = 2
22 / 10 = 2

2 % 10 = 2

Add all of them.. (4 + 3 + 0 + 2 + 2 = 11)




回答4:


You need to divide and take the modulus:

26506 / 10000 = 2
26506 % 10000 = 6506

6506 / 1000 = 6
6506 % 1000 = 506

506 / 100 = 5
506 % 100 = 6

6 / 10 = 0
6 % 10 = 6

6 / 1 = 6

So the result of each division is the digit for that base10 place, in order to get the next lesser significant digit, you take the modulus. Then repeat.




回答5:


If your input is in String:

public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Enter your number: ");


        try{
            BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
            String input = bufferRead.readLine();
            char[] tokens;
            tokens = input.toCharArray();
            int total=0;
            for(char i : tokens){
                total += Character.getNumericValue(i);
            }
            System.out.println("Total: " + total);

            }catch(IOException e){
                e.printStackTrace();
            }            
    }

If your input is in Integers, simple use

    String stringValue = Integer.toString(integerValue);

and plug it in.




回答6:


There are two approaches to this:

UNPACK the number using:(assuming the number remains 5 characters)

int unpack(int number)
{
    int j = 0;
    int x = 0;
    for(j = 0; j < 5; j++){
        x += number % 10;
        number = number / 10;
    }
    return x;
}

put it into a string and select individual characters and parse into Integer:

int sumWithString(String s)
{
    int sum = 0;
    for(int j = 0;j < 5;j++){
        try{
        sum += Integer.parseInt(""+s.charAt(j));
        }catch(Exception e){ }
    }
    return sum;
}



回答7:


    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter number: ");
    String s = scanner.nextLine();

    char[] a = s.toCharArray();
    int total = 0;

    for(char x: a){
        try {
            total += Integer.parseInt(""+x);
        } catch (NumberFormatException e){
            // do nothing
        }
    }

    System.out.println(total);

This will omit any non-number character.



来源:https://stackoverflow.com/questions/12922377/java-integer-addition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!