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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
1100之间共有25个素数
153 370 371 407
90=2*3*3*5
请输入成绩
90
90分,属于A等
请输入第一个数
1
请输入第二个数
2
12的最大公约数为1
12的最小公倍数为2
请输入一串字符:ds12含~  
输入的汉字个数:1
输入的字母个数:2
输入的数字个数:2
输入的空格个数:3
输入的其它字符个数:3
 
Process finished with exit code 0

  

posted @   信方  阅读(927)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)
点击右上角即可分享
微信分享提示