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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 巩彪 初级黑马   /  2012-8-15 00:29  /  1653 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

请编写一个程序,程序的功能是:求Sn=a+aa+aaa+…+aaa…a ,
最后一项为n个a,其中a是一个数字。
例如:a为2、n为5时,则S5=2+22+222+2222+22222。要求a和n都要从键盘输入。

8 个回复

倒序浏览
------刚做完的练习-----
import java.util.Scanner;
public class Sn
{
        public static void main(String[] args)
        {
        System.out.print("求Sn=a+aa+aaa+…+aaa…a的值,请输入a的值:");
        Scanner sc = new Scanner(System.in).useDelimiter("\\s*");//以空格作为分隔符
        int a = sc.nextInt();
        int n = sc.nextInt();
        sc.close();//关闭扫描器
        System.out.println(expressed(2,5)+add(2,5));
        }
        //求和表达式
        private static String expressed(int a,int n)
        {
            StringBuffer sb = new StringBuffer();
                StringBuffer subSB = new StringBuffer();
                for(int i=1;i<n+1;i++)
                {
                  subSB = subSB.append(a);
                  sb = sb.append(subSB);
                  if(i<n)
                        {
                      sb = sb.append("+");
                    }
                        sb.append("=");
                        return sb.toString();
                }
                //求和
       private static long add(int a,int n)
                {
                     long sum = 0;
                         long subSUM = 0;
                         for(int i=1;i<n+1;i++)
                        {
                         subSUM = subSUM*10+a;
                         sum = sum+subSUM;
                         }
                         return sum;
                }
    }
}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
import java.util.Scanner;
public class ForDemo {
public static void main(String[] args) {
  Scanner sc =new Scanner(System.in);
  System.out.println("请输入两个整数");
  int n = sc.nextInt();
  int a = sc.nextInt();
  int sum = 0;
  int sn = 0;
  for(int x=0;x<n;x++)
  {
   sn += a;            //sn = 2; sn = 22; sn=222; sn=2222
   sum += sn;       //2+22+222+2222+22222 = 24690
   a*=10;             //2,20,200,2000
  }
  System.out.println("sum:"+sum);
}
}
这样咋样.

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 杨健yj 于 2012-8-15 00:52 编辑

自己写的代码,里面能排除非法字符,并打印出数组,和最后的结果 ,如果输入over就结束程序
  1. import java.util.Scanner;

  2. public class diGui {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 Scanner sc = null;
  9.                 int num = 0;
  10.                 int n = 0;
  11.                 System.out.println("请输入一个数字");
  12.                 boolean falge = true;
  13.                 while (falge) {
  14.                         sc = new Scanner(System.in);
  15.                         if (sc.hasNextInt()) {
  16.                                 num = Math.abs(sc.nextInt());
  17.                                 System.out.println("请再输入一个数:");
  18.                                 while (falge) {
  19.                                         sc = new Scanner(System.in);
  20.                                         if (sc.hasNextInt()) {
  21.                                                 n = Math.abs(sc.nextInt());
  22.                                                 print(num, n);
  23.                                                 System.out.println(sum(num, n));
  24.                                                 break;
  25.                                         } else if (sc.hasNext("over")) {
  26.                                                 break;
  27.                                         } else {
  28.                                                 System.out.println("请输入正确的数字,请不要输入字符");
  29.                                         }

  30.                                 }
  31.                                 System.out.println("请输入一个数字:");
  32.                                 continue;
  33.                         } else if (sc.hasNext("over")) {
  34.                                 break;
  35.                         } else {
  36.                                 System.out.println("请输入正确的数字,请不要输入字符");
  37.                         }

  38.                 }
  39.         }

  40.         // 打印这个序列
  41.         public static void print(int num, int n) {
  42.                 int i = 1;
  43.                 int[] arr = new int[1024];
  44.                 arr[0] = num;
  45.                 if (n <= 0) {
  46.                         System.out.println("n不能小于等于0!!!");
  47.                 } else {
  48.                         if (n == 1) {
  49.                                 System.out.println("[" + arr[0] + "]");
  50.                         } else {
  51.                                 System.out.print("[");
  52.                                 System.out.print(arr[0] + ",");
  53.                                 int sum = 0;
  54.                                 while (i < n) {
  55.                                         arr[i] = (int) (num * Math.pow(10, i)) + arr[i - 1];
  56.                                         if (i == n - 1) {
  57.                                                 System.out.print(arr[i] + "]");
  58.                                         } else {
  59.                                                 System.out.print(arr[i] + ",");
  60.                                         }
  61.                                         i++;
  62.                                 }
  63.                         }
  64.                 }

  65.         }

  66.         public static int sum(int num, int n) {
  67.                 int i = 1;
  68.                 int[] arr = new int[1024];
  69.                 arr[0] = num;
  70.                 int sum = 0;
  71.                 while (i < n) {
  72.                         arr[i] = (int) (num * Math.pow(10, i)) + arr[i - 1];
  73.                         // System.out.println("---"+arr[i]);
  74.                         i++;
  75.                 }
  76.                 for (int j = 0; j < arr.length; j++) {
  77.                         sum = sum + arr[j];
  78.                 }
  79.                 return sum;
  80.         }
  81. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
import java.util.Scanner;

public class GetNum {
        //定义一个从键盘上获取数据的方法
public int getNum(){
        Scanner sc=new Scanner(System.in);
        int i=sc.nextInt();
        return i;
}
}

public class GetAdd {
        public long getNum(){
                GetNum gn=new GetNum();
                int a=gn.getNum();
                int b=gn.getNum();
                return add(a,b);
        }
        public long add(int a,int b){
        long sum=0;
        int m=0;
        long[]arr=new long[b];
        for(int i=0;i<b;i++){
                arr[i]=(sum+=((int) Math.pow(10, i)*a));
        }
        for(int i=0;i<b;i++){
                m+=arr[i];
        }
        return m;
        }
}
public class Test{

public static void main(String[] args) {
                GetAdd ga=new GetAdd();
                System.out.println(ga.getNum());

        }

}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;

  3. public class TestScanner {
  4.         public static void main(String[] args) {
  5.                 Scanner sc = new Scanner(System.in);
  6.                 System.out.println("请输入两个整数");
  7.                 int n = 0;
  8.                 int a = 0;
  9.                 try {
  10.                         n = sc.nextInt();
  11.                         a = sc.nextInt();
  12.                         int sum = 0;
  13.                         int sn = 0;
  14.                         for (int x = 1; x <= n; x++) {
  15.                                 sum += a; // 计算当前a的和,变成类似222这种特征的数值
  16.                                 sn += sum; // 累计计算结果形式类似:2+22+222……
  17.                                 a *= 10; // 每次计算完成之后让a的值长大10倍
  18.                         }
  19.                         System.out.println("sn:" + sn);// 输出获取的结果
  20.                 } catch (InputMismatchException e) {
  21.                         System.out.println("你输入了非数字的字符");
  22.                 }

  23.         }

  24. }
复制代码
//大一的时候,学C语言的时候应该有这玩意儿的

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
package com;

import java.util.Scanner;

public class Test1 {
        public static void main(String[] args) {
                System.out.println("请在控制台输入 如  2,5 格式的任意数字");
                Scanner s=new Scanner(System.in);//扫描控制台输入的参数
                String str=s.next();//在控制台输入方式为如:2,5    这要注意!!!!!!!!
                String str1[]=str.split(",");//字符串按指定字符拆分
                test1(Integer.parseInt(str1[0]),Integer.parseInt(str1[1]));//调用方法
        }
        public static void test1(int a,int b){
                String str="";
                int c=0;
                String sum="";
                String sum_="sum_";
                for (int i = 0; i < b; i++) {
                        c=Integer.parseInt(str+=a);//把数字通过字符串相加,然后再转成数字
                        sum+=(c+"+");
                        if(i==b-1){
                                sum=sum.substring(0,sum.length()-1);//截取字符串去掉最后的“+”
                                sum_=sum_+String.valueOf(b)+"="+sum;//拼接字符串为sum_5=2+22+222+2222+22222,其中5为输入的第二个参数
                        }
                }
                System.out.println(sum_);
        }
}
回复 使用道具 举报
发的有点快,忘了说两句,你怎么发了两个贴,看来挺急,啊,看到大家不同的方法,很好
回复 使用道具 举报
  • public class Test{
  • public static void main(String[] args) {

  • Test test = new Test();
  • Scanner sc = new Scanner(System.in);
  • System.out.println("请输入两个整数");
  • int n = 0;
  • int a = 0;
  • try {
  • n = sc.nextInt();
  • a = sc.nextInt();
  • }
  • catch (InputMismatchException e) {
  • System.out.println("你输入了非数字的字符");
  • }

  • int sn = 0;

  • sn =test.aa(n,a);    //aa这个函数被调用
  • System.out.println("sn:" + sn);// 输出获取的结果
  • }

//这里定义了一个 算法
  • public int aa(int n ,int a )
    {
  • int sum = 0;
  • for (int x = 1; x <= n; x++) {
  • sum += a;
  • sn += sum;
  • a *= 10;
  • }
  • return sn;
  • }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马