在java上将倒数第二个相加 [英] sum the second to the last on java

查看:29
本文介绍了在java上将倒数第二个相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在java上打印每个整数的第2位到最后一位数字的总和?

How can I print the sum of the 2nd to the last digit of each integer on java?

(因此,8 将被打印,因为 1 + 3 + 4 是 8 ,35 将被打印,因为 3453 + 65324 + 354)在以下程序中:* 不使用 if 语句 *

(so, 8 would be printed since 1 + 3 + 4 is 8 , and 35 would be printed since 3453 + 65324 + 354) in the following Program: * without using if statements *

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.print((num1/10) % 10 + " + " + (num2/10) % 10 + " + " + (num3/10) % 10 + " = " + sumSecToLast);


      }//main
}//Pr6

推荐答案

很抱歉给您带来不便.我的问题被误解了.我的意思是我想写一个代码来找到三个不同整数的第二到最后一位数字的总和.例如:如果用户输入了 15、34 和 941,在这种情况下,倒数第二个数字将是 1、3 和 4.因此,它们的小计将是 1+3+4 = 8.

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);
    System.out.println();
    
  }//main
}//Pr6

这篇关于在java上将倒数第二个相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆