Spark记录-Scala基础程序实例

object learn {
 def main(args:Array[String]):Unit={
   println("请输入两个数字:")
   var a:Int=Console.readLine().toInt
   var n:Int=Console.readLine().toInt
   println(expressed(a,n)+add(a,n));
   val n1:Int = 100;
   compNumber(n1);
   println("请输入三个数字")
   var n2:Int=Console.readLine().toInt
   var n3:Int=Console.readLine().toInt
   var n4:Int=Console.readLine().toInt
   sort(n2,n3,n4);
   //九九乘法表
   for(i<-1 until 10){
     for(j<-1 until i+1)
     print(j+"*"+i+"="+(j*i)+" ");
     println()
   }
   val n5:Int = 5;
   printStar(n5);

 }
  //求和表达式
  private def expressed(a:Int,n:Int):String={
    var sb:StringBuffer = new StringBuffer();
    var subSB:StringBuffer = new StringBuffer();
    for(i<-1 until  n+1){
      subSB = subSB.append(a);
      sb = sb.append(subSB);
      if(i<n)
        sb = sb.append("+");
    }
    sb.append("=");
    return sb.toString();
  }
  //求和
  private def add(a:Int,n:Int):Long={
    var sum:Long = 0;
    var subSUM:Long = 0;
    for(i<-1 until n+1){
      subSUM = subSUM*10+a;
      sum = sum+subSUM;
    }
    return sum;
  }
  //求完数
  private def compNumber(n:Int):Unit={
    var count:Int = 0;
    println(n+"以内的完数:");
    for(i<-1 until n+1){
      var sum:Int = 0;
      for(j<-1 until i/2+1){
        if((i%j)==0){
          sum += j;
          if(sum==i){
            print(i+" ");
            count=count+1;
            if((count%5)==0)
              println();
          }
        }
      }
    }
  }
  //比较两个数的大小
  private def sort(x:Int,y:Int,z:Int):Unit={
    var x1:Int=x;
    var y1:Int=y;
    var z1:Int=z;
    if(x1>y1){
      var t:Int = x1;
      x1 = y1;
      y1 = t;
    }
    if(x1>z1){
      var t:Int = x1;
      x1 = z1;
      z1 = t;
    }
    if(y1>z1){
      var t:Int = z1;
      z1 = y1;
      y1 = t;
    }
    println("排序结果是:"+x1+" "+y1+" "+z1);
  }
  //打印星星
  private def printStar(n:Int):Unit={
    //打印上半部分
    for(i<-0 until n){
      for(j<-0 until 2*n){
        if(j<n-i)
          System.out.print(" ");
        if(j>=n-i && j<=n+i)
          System.out.print("*");
      }
      println();
    }
    //打印下半部分
    for(i<-1 until n){
      print(" ");
      for(j<-0 until 2*n-i){
        if(j<i)
          print(" ");
        if(j>=i && j<2*n-i-1)
          print("*");
      }
           println();
    }
  }
}
请输入两个数字:
1
2
1+11=12
100以内的完数:
6 24 28 请输入三个数字
2
31
1
排序结果是:1 2 31
1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 
     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *

Process finished with exit code 0

 

 

posted @ 2017-11-22 10:35  信方  阅读(363)  评论(0编辑  收藏  举报