Scala中的while循环使用关键字while,其语法格式如下:

while (condition) {
   // code to be executed
}

其中,condition是一个布尔表达式,如果它的值为true,则会执行花括号内的代码块,否则跳过该循环。

例如,下面的代码使用while循环输出从1到10的数字:

var i = 1
while (i <= 10) {
  println(i)
  i += 1
}

输出结果为:

1
2
3
4
5
6
7
8
9
10

在循环中,我们使用变量i来追踪循环的进度。每次循环时,我们打印出i的值,并将其递增1。当i的值达到10时,循环结束。

Scala while循环

原文地址: https://www.cveoy.top/t/topic/e815 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录