java - Sum digits in an integer -


i new java, , working sum integer's individual numbers. believe problem lies in formula using obtain values of individual numbers, hoping gain insight brilliant people here! please note code works in test cases have used 1 exception: integer 234. returning result of 10 sum.

import java.util.scanner;  public class exercise2_6 {     public static void main(string[] args) {         scanner input = new scanner(system.in);          system.out.print("enter integer between 0 , 1000: ");          int x = input.nextint();         int firstnum = x / 100;         int secondnum = x % 10;         int thirdnum = secondnum  % 10;         int fourthnumber = x / 10;         int sum = firstnum + secondnum + thirdnum;          system.out.println("the sum of digits in " + x +" " + sum);        }} 

one problem

int thirdnum = secondnum  % 10; 

since secondnum between 0 , 9, make thirdnum same secondnum.

i suspect wanted

int secondnum = x % 100 / 10; int thirdnum = x % 10; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -