asdtiang的博客 感谢blogjava提供的博客交流平台

 ```var nums = [5, 7, 3, 9]; var total = { var sum = 0; for (a in nums) { sum += a }; sum; } println("Total is {total}.");```

 `Total is 24.`

 ```{ var sum = 0; for (a in nums) { sum += a }; sum; }```

 ```def age = 8; var ticketPrice; if (age < 5 ) { ticketPrice = 0; } else if (age < 12 or age > 65) { ticketPrice = 5; } else { ticketPrice = 10; } println("Age: {age} Ticket Price: {ticketPrice} dollars.");```

 `Age: 8 Ticket Price: 5 dollars.`

 ```if (age < 5 ) { ticketPrice = 0; } else if (age < 12 or age > 65) { ticketPrice = 5; } else { ticketPrice = 10; }```

 ```if (age < 5 ) { ticketPrice = 0; } else if (age < 12 or age > 65) { ticketPrice = 5; } else { ticketPrice = 10; }```

 ```if (age < 5 ) { ticketPrice = 0; } else if (age < 12 or age > 65) { ticketPrice = 5; } else { ticketPrice = 10; }```

 `ticketPrice = if (age < 5) 0 else if (age < 12 or age > 65) 5 else 10;`

“序列”一课讲授了一种用来声明形成等差数列的数字序列的简化表示法。

 `var num = [0..5];`

 ```var nums = [1..10 step 2]; println(nums);```

 `[ 1, 3, 5, 7, 9 ]`

 ```var nums = [10..1 step -1]; println(nums);```

 `[ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]`

 ```var nums = [10..1 step 1]; println(nums);```

 ```range.fx:1: warning: empty sequence range literal, probably not what you meant. var nums = [10..1 step 1]; ^ 1 warning```

 ```var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]; for (day in days) { println(day); }```

 ```Mon Tue Wed Thu Fri Sat Sun```

 ```for (day in days) { println(day); }```

`days` 变量是要由 `for` 表达式处理的输入序列的 名称：

 ```for (day in days) { println(day); }```

`for` 表达式遍历该序列时，`day` 变量用来存放当前项：

 ```for (day in days) { println(day); }```

 ```// Resulting sequence squares the values from the original sequence. var squares = for (i in [1..10]) i*i; // Resulting sequence is ["MON", "TUE", "WED", and so on...] var capitalDays = for (day in days) day.toUpperCase();```

 ```var count = 0; while (count < 10) { println("count == {count}"); count++; }```

 ```count == 0 count == 1 count == 2 count == 3 count == 4 count == 5 count == 6 count == 7 count == 8 count == 9```

 ```var count = 0; while (count < 10) { println("count == {count}"); count += 1; }```

 ```var count = 0; while (count < 10) { println("count == {count}"); count += 1; }```

While 表达式的主体会输出 `count` 的当前值，然后将 `count` 的值加 1：

 ```var count = 0; while (count < 10) { println("count == {count}"); count += 1; }```

`count` 等于 10 时，循环退出。要创建一个无限循环，请将 `true` 关键字放在左小括号和右小括号之间，如 `while(true){}` 中所示。

`break``continue` 表达式与循环表达式有关。这两个表达式会影响循环迭代：`break` 完全放弃循环，而 `continue` 仅放弃当前迭代。

 ```for (i in [0..10]) { if (i > 5) { break; } if (i mod 2 == 0) { continue; } println(i); }```

 ```1 3 5```

 ```if (i > 5) { break; }```

 ```if (i mod 2 == 0) { continue; }```

 ```import java.lang.Exception; foo(); println("The script is now executing as expected... "); function foo() { var somethingWeird = false; if(somethingWeird){ throw new Exception("Something weird just happened!"); } else { println("We made it through the function."); } }```

 ```We made it through the function. The script is now executing as expected...```

 ```Exception in thread "main" java.lang.Exception: Something weird just happened! at exceptions.foo(exceptions.fx:10) at exceptions.javafx\$run\$(exceptions.fx:3)```

 ```try { foo(); } catch (e: Exception) { println("{e.getMessage()} (but we caught it)"); }```

 ```Something weird just happened! (but we caught it) The script is now executing as expected...```

 ```try { foo(); } catch (e: Exception) { println("{e.getMessage()} (but we caught it)"); } finally { println("We are now in the finally expression..."); }```

```Something weird just happened! (but we caught it)
We are now in the finally expression...
The script is now executing as expected...```

 只有注册用户登录后才能发表评论。 网站导航: 相关文章:

posts - 80, comments - 24, trackbacks - 0, articles - 32