How can I sum the second digit to the last digit in an integer on java? -


Can you tell me how "sumSecTolastD" operator can fix this program to do the following: < Second digit in the last digit in the integer (for example: 324 + 1) since (1 + 4) + (6 + 4) + (8 + 7 + 6) = 1 (1 + 2) = 1 564 + 9876 = 2) 29) 2 - If I include only one digit, it is still capable of giving me the amount (for example: 1 + 3 + 4 = 8).

  Import java.util. *; Public square pr66 {public static zero main (string [] args) {scanner scan = new scanner (System.in); Int number 1; Int number 2; Int number 3; Int sumLastD; Int sumSecTolastD; System.out.print ("Please type an integer:"); Num1 = scan.nextInt (); System.out.print ("Please type an integer:"); Num2 = scan.nextInt (); System.out.print ("Please type an integer:"); Num3 = scan.nextInt (); SumLastD = num1% 10 + num2% 10 + num3% 10; Println (); System.out.println ("- LastDigitSum:" + num1% 10 + "+" + 2 + 10 + "+ + + 3 + 10 +" = "+ + sumLastD); if (sumLastD% 2 == 0) System.out.println ("- LastDigitSum: is an integer"); Else System.out.println ("- LastDigitSum: is a strange integer"); Println (); SumSecTolastD = (num1 / 10)% 10 + (num2 / 10)% 10 + (num3 / 10)% 10; System.out.println ("- SecToLastSum:" + (num1 / 10)% 10 + "+" + (num2 / 10)% 10 + "+ + + (Num3 / 10)% 10 + "=" + sumSecTolastD); If (sumSecTolastD% 2 == 0) System.out.println ("- SecToLastSum: is a single integer"); Else System.out.println ("- SecToLastSum: is a strange integer"); } // main} // PR66  

This last digit indicates:

  sumLastD = num1% 10 + num2% 10 + num3% 10;  

To get the last and second digits from the last digits, add your calculation to the last digits of the second to the other:

  sumSecTolastD = (Num1 / 10)% 10 + (num2 / 10)% 10 + (num3 / 10)% 10 + sum_lastD;  

This will require additional checks to work with single digit numbers, and make your code too large:

  sumLastD = (String ValueOf (Num1) Length ()> 1? Num1% 10: num1) + (string.valouf (num2). Length ()> 1? Num2% 10: num2) + (string.valouf (num3). & Gt; 1? Num3% 10: num3);  

Yes, you can put it on multiple lines, the compiler ignores unnecessary white space. Modify the following for

sumSecTolastD :

  sumSecTolastD = (String.valueOf (num1). Length ()> 2? (Num1 / 10)% 10: num1) + (string.value (num2). Length ()> 2? (Num2 / 10)% 10: num2) + (string.valouf (num3). Length ()> 2? ( Num3 / 10)% 10: num3) + sum_lastD;  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -