当前位置:网站首页>Scala-day03- operators and loop control
Scala-day03- operators and loop control
2022-06-26 12:09:00 【There will always be daylight】
One : Arithmetic operator
1: Basic grammar

For division marks , There is a difference between integer division and decimal division , Divide integers , Keep only the integer parts , And discard the decimal part .
Modulo for a number a%b, and Java As like as two peas .
2: Case study
package chapter03
object TestArithmetic {
def main(args: Array[String]): Unit = {
var r1:Int = 10/3 //3
println("r1=" + r1)
var r2:Double = 10 / 3 //3.0
println("r2=" + r2)
var r3:Double = 10.0 / 3 //3.3.333
println("r3=" + r3)
println("r3=" + r3.formatted("%.2f")) // Keep the decimal point 2 position , Use rounding
var r4 = 10 % 3 //1
println("r4=" + r4)
}
}
Two : Relational operator
1: Basic grammar

2.1: Case study 1
package chapter03
object TestRelation {
def main(args: Array[String]): Unit = {
var a:Int = 2
var b:Int = 1
println(a > b) //true
println(a >= b) //true
println(a <= b) //false
println(a < b) //false
println("a==b" + (a == b)) //false
println(a != b) //true
}
}
2.2: Case study 2
java in :== Compare the values of the two variables themselves , That is, the addresses of two objects in memory
equals Compare whether the contents contained in the string are the same
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
Output result :
false
trueScala:== More similar to Java Medium equals
def main(args: Array[String]): Unit = {
val s1 = "abc"
val s2 = new String("abc")
println(s1 == s2)
println(s1.eq(s2))
}
Output result :
true
false3、 ... and : Logical operators
1: Basic grammar , hypothesis A by true,B by false

2: Case study
package chapter03
object TestLogic {
def main(args: Array[String]): Unit = {
var a = true
var b = false
println("a&&b=" + (a&&b)) //false
println("a||b=" + (a || b)) //true
println("!(a&&b)" + (!(a && b))) //false
}
}
Four : Assignment operator
1: Basic grammar :Scala There is no ++、-- The operator , Can pass +=、-= To achieve the same effect
2: Case study
object TestAssignment {
def main(args: Array[String]): Unit = {
var r1 = 10
r1 += 1 // No,
r1 -= 2 // No,
}
}5、 ... and : An operator
1: Basic grammar :a:60,b:13

2: Case study
object TestPosition {
def main(args: Array[String]): Unit = {
// test : 1000 << 1 =>10000
var n1 :Int =8
n1 = n1 << 1
println(n1)
}
}6、 ... and : Conditional branch
1: Single branch
if ( Conditional expression ) {
Execute code block
}
2: Double branch
if ( Conditional expression ) {
Execute code block 1
} else {
Execute code block 2
}
3: Multiple branches
if ( Conditional expression 1) {
Execute code block 1
} else if ( Execute expression 2) {
Execute code block 2
}...
else {
Execute code block n
}
4: Nesting branches
if () {
if (){
}else{
}
}
7、 ... and :FOR loop
1: Range data cycle (To)
for (i <- 1 to 3){
print(i + " ")
}
println()
//1.i The variable representing the loop ,<- Regulations to
//2.i Will be from 1-3 loop , Close back and forth 2: Range data cycle (Unitl)
for (i <- 1 until 3) {
print(i + " ")
}
println()
//1. The difference between this method and the previous one is i It's from 1 To 3-1
//2. Front closed back open return 3: Cycle guard
for (i <- 1 to 3 if i != 2) {
print(i + " ")
}
println()
//1. Cycle guard . The protection type is true Then go inside the circulatory body , When false Then skip , Be similar to continue
//2. The following code is equivalent
for (i <- 1 to 3){
if (i != 2){
print(i + " ")
}
}4: Cycle step
for (i <- 1 to 10 by 2) {
print("i=" + i)
}
// The output result is 1,3,5,7,95: Nested loop
for (i <- 1 to 3;j < -1 to 3) {
println("i=" + i + "j=" + j)
}
for (i <- 1 to 3) {
for (j <- 1 to 3){
println("i=" + i + "j=" +j)
}
}
// The two are equivalent 6: Introduce variables
for (i <-1 to 3;j = 4-i) {
println("i=" + i + "j=" + j)
}
for (i <- 1 to 3) {
var j = 4 -i
println("i=" + i + "j=" + j)
}
// Both equal 7: Loop return value
var res = for (i <- 1 to 10) yield {
i * 2
}
println(res)
// Return the result processed in the traversal result to a new vector Collection , Use yield keyword
// result vector(2,4,6,8,10,12,14,16,18,20)
8: Print in reverse order
for (i <- 1 to 10 reverse) {
println(i)
}
// Print in reverse order 10 To 18、 ... and :while and do..while Cycle control
1: And Java The usage is the same
2:while Cycle control
object TestWhile {
def main(args: Array[String]): Unit = {
var i = 0
while (i < 10) {
println("i = " + i)
i += 1
}
}
}3:do..while control
object TestWhile {
def main(args: Array[String]): Unit = {
var i = 0
do {
println("i =" + i)
i += 1
} while (i < 10)
}
}Nine : Cycle break
1: Exit the loop in an abnormal way
def main(args: Array[String]): Unit = {
try {
for (elem < 1 to 10) {
println(elem)
if (elem == 5) throw new RuntimeException
}
}catch {
case e =>
}
println(" Normal end of cycle ")
}2:Scala Built-in function , Exit loop
import scala.util.control.Breaks
def main(args: Array[String]): Unit = {
Breaks.breakable {
for (elem < 1 to 10) {
println(elem)
if (elem == 5) Breaks.break()
}
}
println(" Normal end of cycle " )
}3: Yes break Omit
import scala.util.control.Breaks._
object TestBreak {
def main(args: Array[ String]): Unit = {
breakable
for (elem < 1 to 10) {
println(elem)
if (elem == 5) break
}
}
println(" Normal end of cycle " )
}
}边栏推荐
- Basic use of express in nodejs
- Build document editor based on slate
- Is it safe to open a securities account in general
- Redis best practices? If I don't feel excited after reading it, I will lose!!
- 我想知道,股票开户有哪些优惠活动?网上开户是否安全么?
- VMware virtual machine bridging mode can not access the campus network "suggestions collection"
- Loggie encoding and newline character test
- 【毕业季·进击的技术er】忆毕业一年有感
- 19:第三章:开发通行证服务:2:在程序中,打通阿里云短信服务;(仅仅是打通阿里云短信服务器,不涉及具体的业务开发)
- 2021 q3-q4 investigation report on the use status of kotlin multiplatform
猜你喜欢

4. N queen problem

深度学习中的FLOPs和Params如何计算

leetcode 715. Range 模块 (hard)

【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构

量化初级 -- akshare获得股票代码,最简策略

Redis best practices? If I don't feel excited after reading it, I will lose!!

Ctfshow web getting started command execution web75-77

Matlab programming example: how to count the number of elements in a cell array

Re recognized! Know that Chuangyu has been selected as one of the first member units of the "business security promotion plan"
![Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]](/img/b2/f09e220918a85b14a1993aa85f7720.png)
Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]
随机推荐
Leetcode 78. 子集 and 90. 子集 II
International beauty industry giants bet on China
深圳市福田区支持文化创意产业发展若干措施
What are the top ten securities companies? Is it safe to open a mobile account?
修改calico网络模式为host-gw
How do consumer goods enterprises formulate membership interests?
Statistical genetics: Chapter 1, basic concepts of genome
Solidworks渲染技巧如何不显示边线--显示样式设定
HUST网络攻防实践|6_物联网设备固件安全实验|实验二 基于 MPU 的物联网设备攻击缓解技术
MQTT断开重连
Redux related usage
How to do well in member marketing three steps to teach you to understand member management
Build document editor based on slate
fastjson的JSONArray和JSONObject[通俗易懂]
2016年四川省TI杯电子设计竞赛B题
深度学习中的FLOPs和Params如何计算
开通证券账户需要注意事项 开户安全吗
Omnichannel membership - tmall membership 1: opening tutorial
.net中,日志组件 Nlog,SerialLog, Log4Net的用法
NFS shared storage service installation