A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘伯阳 中级黑马   /  2012-5-31 17:21  /  1704 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

从键盘输入一个四位数,将这个四位数的个位、十位、百位、千位相加并输出,我写的代码在下面,但是总是觉得好麻烦,有没有朋友有更好的算法,甚至是随便输入一个n位整数,将他们各位相加结果输出呢?
import java.util.Scanner;


public class Test2 {
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                System.out.println("请输入一个四位数:");//假设为1234
                Scanner in = new Scanner(System.in);
                int x = in.nextInt();//1234
                double i = x/1000;//1.234
                double i1 = Math.floor(i);//1.0
                double i2 = x-i1*1000;//234
                double j = i2/100;//2.34
                double j1 = Math.floor(j);//2
                double j2 = x-i1*1000-j1*100;//34.0
                double k = j2/10;//3.4
                double k1 = Math.floor(k);//3
                double k2 = x-i1*1000-j1*100-k1*10;//4
                int y = (int)(i1+j1+k1+k2);
                System.out.println(y);
        }

}

5 个回复

倒序浏览
import java.util.Scanner;
public class Test2 {
        public static void main(String[] args) {
              
                System.out.println("请输入一个四位数:");
                Scanner in = new Scanner(System.in);
                int x = in.nextInt();
                                        int qian = x / 1000;
                                        int bai = (x % 1000) / 100;
                                        int shi= (x / 10 ) % 10;
                                        int ge = (x %100) % 10;
                int y = (int)(qian+bai+shi+ge);
                System.out.println(y);
        }
好像能强点{:soso_e113:}

}
回复 使用道具 举报
本帖最后由 王渠 于 2012-5-31 18:44 编辑
  1. import java.util.Scanner;
  2. public class sum
  3. {
  4.         public static void main(String[] args){
  5.               Scanner input = new Scanner(System.in);
  6.               System.out.print("Enter an integer:")
  7.               int number = input.nextInt();
  8.               int sum = 0;
  9.               int temp = number;
  10.               while(temp!=0){
  11.                    sum += temp%10;
  12.                    temp = temp/10;
  13.                }
  14.         System.out.println(sum);
  15. }
  16. }
复制代码
回复 使用道具 举报
  1. import java.util.*;

  2. class ListIteratorDemo {
  3.         public static void main(String[] args) {
  4.                    System.out.println("请输入一个四位数:");//假设为1234
  5.            Scanner in = new Scanner(System.in);
  6.            int x = in.nextInt();//1234
  7.           String num = String.valueOf(x);  //num 转换为字符串操作
  8.          ArrayList<Character> al  = new ArrayList<Character>();
  9.          
  10.          int total = 0;
  11.          
  12.           for (int i =0; i<num.length(); i++){
  13.                   al.add(num.charAt(i));
  14.           }
  15.          
  16.           for(Iterator<Character> it = al.iterator(); it.hasNext();){
  17.                   total+= Integer.parseInt(it.next().toString());
  18.           }
  19.           System.out.println( total );
  20.          
  21.         }
  22. }
复制代码
这个貌似扩展性要高一些
回复 使用道具 举报
王渠 发表于 2012-5-31 18:14

嗯 这个不错 但是你代码中
int number = input.nextInt();
System.out.print("Enter an integer:")

这两句的顺序应当调换一下否则程序不能运行 谢谢了~
回复 使用道具 举报
public static void main(String[] args)

        {
                Scanner sc = new Scanner(System.in);
                System.out.print("输入一个任意长度字符:");
                String str = sc.next();
                BigInteger sum = BigInteger.ZERO;
                BigInteger[] num = new BigInteger[str.length()];
                for (int i = 0; i < str.length(); i++) {

                        num[i] = new BigInteger((String) str.substring(i, i + 1));
                }
                for (int i = 0; i < num.length; i++) {
                        sum = sum.add(num[i]);
                }
                System.out.print(sum);
        }

支持任意字符长度的运算。

点评

这个好叼  发表于 2012-5-31 19:58
嗯 实现了~ 这个帅 谢谢了! 我去研究下你写的  发表于 2012-5-31 18:34
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马