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

BAS

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

BAS

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]

編集
0

コメントを追加

0

関連記事

how can i get single digit from four digit

how can I get nth digit in a BigInteger?

How can I add Digit places to my results?

How to split string after the last digit

How to convert integer whole value to 3 digit dot seperated string

How can I write a regex that finds everything but 4 digit numbers like 2000 or 1990 or 1234?

I have to change my number last digit and first digit but without using functions only with integers or loops. For example from 12345 to 52341

How to display digit in android?

Why can't I name a sigilless variable v+digit?

Getting the last digit of number counting up

Positioning list item numbers by last digit...?

Round last digit to 0 , 5 or 9 in PHP

how to query the highest digit in a search?

How to test if a string character is a digit?

Hive: how to display and store a 20-digit integer without using scientific notation?

Generating a three-digit number Java

How can I get textbox value into array and convert into integer in java

Rounding of last digit changes after Windows .NET update

RegEx to get the alphabets or alphabets including the last digit with space

How to use M-<Number> to type a digit?

how append a random digit at the end of First Name

How to concatenate a digit in the different indexes of a number?

How to compute a check digit for a large number in R?

In Python how to find the least significant digit

How to list all possible solutions for missing digit?

how to calculate digits of a number without the right digit

GREP: how to match positive or negative digit?

How to grep any digit from sentence

how to check to make sure that each character of the command line argument is a decimal digit (i.e., 0, 1, 2, etc.)

TOP 一覧

  1. 1

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    Ansibleで複数行のシェルスクリプトを実行する方法

  4. 4

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  5. 5

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  6. 6

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  7. 7

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  8. 8

    Chromeウェブアプリのウェブビューの高さの問題

  9. 9

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  10. 10

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  11. 11

    Reactでclsxを使用する方法

  12. 12

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  13. 13

    MLでのデータ前処理の背後にある直感

  14. 14

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

  15. 15

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  16. 16

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  17. 17

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

  18. 18

    テキストフィールドの値に基づいて UIslider を移動します

  19. 19

    BLOBストレージからデータを読み取り、Azure関数アプリを使用してデータにアクセスする方法

  20. 20

    PowerShellの分割ファイルへのヘッダーの追加

  21. 21

    ソートされた検索、ターゲット値未満の数をカウント

ホットタグ

アーカイブ