Spark记录-Scala记录(基础程序例子)
import scala.util.control._ object learnning { def main(args:Array[String]):Unit={ val n:Int=10 println("第"+n+"个月兔子总数为"+fun(n)) val n1:Int=1 val n2:Int=100 var count:Int=0 //统计素数个数 for(i:Int<-n1 until n2){ if(isPrime(i)){ count=count+1; System.out.print(i+" "); if(count%10==0){ println(); } } } println(); println("在"+n1+"和"+n2+"之间共有"+count+"个素数"); for(i:Int<- 100 until 1000){ if(isLotus(i)) print(i+" "); } println(); val n3:Int=90; decompose(n3); System.out.println("请输入成绩"); var content:Int = Console.readLine().toInt grade(content); println("请输入第一个数"); var n6:Int = Console.readLine().toInt println("请输入第二个数"); var n7:Int = Console.readLine().toInt max_min(n6,n7); System.out.print("请输入一串字符:"); val str:String = Console.readLine().toString countstring(str); } private def fun(n:Int):Int={ if(n==1 || n==2) return 1; else return fun(n-1)+fun(n-2); } //判断素数 private def isPrime(n:Int):Boolean={ var flag:Boolean = true; if(n==1) flag = false; else { val loop = new Breaks; loop.breakable { for (i:Int<- 2 to Math.sqrt(n).toInt) { if ((n % i) == 0 || n == 1) { flag = false; loop.break() } else flag = true; } } } return flag; } //判断水仙花数 private def isLotus(lotus:Int):Boolean={ var m:Int = 0; var n:Int = lotus; var sum:Int = 0; m = n/100; n -= m*100; sum = m*m*m; m = n/10; n -= m*10; sum += m*m*m + n*n*n; if(sum==lotus) return true; else return false; } private def decompose(n:Int):Unit={ print(n+"="); var n4:Int=n; val loop = new Breaks; loop.breakable { for (i <- 2 until n4 + 1) { while (n4 % i == 0 && n4 != i) { n4 /=i; print(i + "*"); } if (n4 == i) { println(i); loop.break(); } } } } //成绩等级计算 private def grade(n:Int):Unit={ if(n>100 || n<0) System.out.println("输入无效"); else{ var str:String = if(n>=90)"分,属于A等" else "属于B等"; System.out.println(n+str); } } //求最大公约数和最小公倍数 private def max_min(m:Int,n:Int){ var temp:Int = 1; var yshu:Int = 1; var bshu:Int = m*n; var m1:Int=m; var n1:Int=n; if(n1<m1){ temp = n1; n1 = m1; m1 = temp; } while(m1!=0){ temp = n1%m1; n1 = m1; m1 = temp; } yshu = n1; bshu /= n1; System.out.println(m+"和"+n+"的最大公约数为"+yshu); System.out.println(m+"和"+n+"的最小公倍数为"+bshu); } //统计输入的字符数 private def countstring(str:String):Unit= { val E1: String = "[\u4e00-\u9fa5]"; //汉字 val E2: String = "[a-zA-Z]"; val E3: String = "[0-9]"; val E4: String = "\\s"; //空格 var countChinese: Int = 0; var countLetter: Int = 0; var countNumber: Int = 0; var countSpace: Int = 0; var countOther: Int = 0; var array_Char = str.toCharArray(); //将字符串转化为字符数组 var n: Int = array_Char.length; for (i: Int <- 0 until array_Char.length) { var array_String:String= array_Char(i).toString(); if (array_String.matches(E1)) { countChinese = countChinese + 1; } else if (array_String.matches(E2)) { countLetter = countLetter + 1; } else if (array_String.matches(E3)) { countNumber = countNumber + 1; } else if (array_String.matches(E4)) { countSpace = countSpace + 1; } else { countOther = countOther + 1; } } println("输入的汉字个数:" + countChinese); println("输入的字母个数:" + countLetter); println("输入的数字个数:" + countNumber); println("输入的空格个数:" + countSpace); println("输入的其它字符个数:" + countSpace); } }
第10个月兔子总数为55 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 在1和100之间共有25个素数 153 370 371 407 90=2*3*3*5 请输入成绩 90 90分,属于A等 请输入第一个数 1 请输入第二个数 2 1和2的最大公约数为1 1和2的最小公倍数为2 请输入一串字符:ds12含~ 输入的汉字个数:1 输入的字母个数:2 输入的数字个数:2 输入的空格个数:3 输入的其它字符个数:3 Process finished with exit code 0