Spark记录-Scala记录(基础程序例子)
Posted 信方互联网硬汉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("请输入一串字符:"); var 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; var array_String=new Array[String](n); //汉字只能作为字符串处理 for (i: Int <- 0 until array_Char.length) { array_String(i)= array_Char(i).toString(); //遍历字符串数组中的元素 for (s <- array_String) { if (s.matches(E1)) { countChinese = countChinese + 1; } else if (s.matches(E2)) { countLetter = countLetter + 1; } else if (s.matches(E3)) { countNumber = countNumber + 1; } else if (s.matches(E4)) { countSpace = countSpace + 1; } else { countOther = countOther + 1; } } System.out.println("输入的汉字个数:" + countChinese); System.out.println("输入的字母个数:" + countLetter); System.out.println("输入的数字个数:" + countNumber); System.out.println("输入的空格个数:" + countSpace); System.out.println("输入的其它字符个数:" + countSpace); } } }
以上是关于Spark记录-Scala记录(基础程序例子)的主要内容,如果未能解决你的问题,请参考以下文章