Spark记录-Scala基础程序实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 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 n 1 : Int = 100 ; compNumber(n 1 ); println( "请输入三个数字" ) var n 2 : Int = Console.readLine().toInt var n 3 : Int = Console.readLine().toInt var n 4 : Int = Console.readLine().toInt sort(n 2 ,n 3 ,n 4 ); //九九乘法表 for (i<- 1 until 10 ){ for (j<- 1 until i+ 1 ) print(j+ "*" +i+ "=" +(j*i)+ " " ); println() } val n 5 : Int = 5 ; printStar(n 5 ); } //求和表达式 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 x 1 : Int = x; var y 1 : Int = y; var z 1 : Int = z; if (x 1 >y 1 ){ var t : Int = x 1 ; x 1 = y 1 ; y 1 = t; } if (x 1 >z 1 ){ var t : Int = x 1 ; x 1 = z 1 ; z 1 = t; } if (y 1 >z 1 ){ var t : Int = z 1 ; z 1 = y 1 ; y 1 = t; } println( "排序结果是:" +x 1 + " " +y 1 + " " +z 1 ); } //打印星星 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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 请输入两个数字: 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 |
分类:
Spark技术相关
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)