Can you tell me how Can I fix this program to make the "sumSecTolastD" operator does the following:
1- sum the second digit to the last digit in an integer (for example: 324 + 564 + 9876 = 29 since (2 + 4) + (6 + 4) + (8 + 7 + 6) = 29).
2- if I insert one digit only, it still able to give me the sum ( for example: 1 + 3 + 4 = 8).
import java.util.*;
public class Pr66{
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
int num1;
int num2;
int num3;
int sumLastD;
int sumSecTolastD;
System.out.print("Please write an integer: ");
num1 = scan.nextInt();
System.out.print("Please write an integer: ");
num2 = scan.nextInt();
System.out.print("Please write an integer: ");
num3 = scan.nextInt();
sumLastD = num1 % 10 + num2 % 10 + num3 % 10;
System.out.println();
System.out.println("- LastDigitSum: " + num1 % 10 + " + " + num2 % 10 + " + " + num3 % 10 + " = " + sumLastD);
if (sumLastD % 2 == 0)
System.out.println("- LastDigitSum: is an even integer");
else
System.out.println("- LastDigitSum: is an odd integer");
System.out.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 an even integer");
else
System.out.println("- SecToLastSum: is an odd integer");
}//main
}//Pr66
Sorry for inconvenience. My question was misunderstood. I meant that I want to write a code to find the sum of the 2nd to the last digit of a three different integers. For ex: if the user entered 15, 34, and 941, which in this case the 2nd to the last digit will be 1, 3, and 4. Therefore, the subtotal of them will be 1+3+4 = 8.
I found out the answer and I wanted to share it with everyone, and also I would like to thank all of those who tried to help. thank you..
import java.util.*;
public class Pr6{
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
int num1;
int num2;
int num3;
int sumSecToLast;
System.out.print("Please write an integer: ");
num1 = scan.nextInt();
System.out.print("Please write an integer: ");
num2 = scan.nextInt();
System.out.print("Please write an integer: ");
num3 = scan.nextInt();
sumSecToLast = ((num1/10) % 10) + ((num2/10) % 10) + ((num3/10) % 10);
System.out.println("The subtotal of the 2nd to the last digit = " + sumSecToLast);
if (sumSecToLast % 2 == 0)
System.out.println(sumSecToLast + " is an even number.");
else
System.out.println(sumSecToLast + " is an odd number.");
}//main
}//Pr6
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加