Scala Process Control

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/chen18677338530/article/details/91440145

Process Control

In the program, the program that runs the process control decision program is how to implement it.

Process Control Description

Scala language, the Java language control structures and control structure is basically the same, the code writing style and way of understanding are not much difference.

Process control scores

  • Sequence control
  • Branch control
  • Loop control

Description sequence control

Here Insert Picture Description

Branch control

Let selective implementation of the program, there are three branches of control

  • Single branch
  • Dual Branch
  • Multi-Branch

Single branch

Here Insert Picture Description

import scala.io.StdIn

object Demo7 {

  def main(args: Array[String]): Unit = {

    println("您的年龄:")
    var age:Int = StdIn.readInt()

    if (age >= 18){
      println("成年人")
    }
  }
}

Dual Branch

Here Insert Picture Description
Here Insert Picture Description

import scala.io.StdIn

object Demo7 {

  def main(args: Array[String]): Unit = {

    println("您的年龄:")
    var age:Int = StdIn.readInt()

    if (age >= 18){
      println("成年人")
    } else {
      println("未成年人")
    }
  }
}

Multi-Branch

if (条件表达式1){
执行代码块1
} else if (条件表达式2){
执行代码块2
}
......
else{
执行代码块n
}

Here Insert Picture Description

import scala.io.StdIn

object Demo8 {
  def main(args: Array[String]): Unit = {

    println("输入您的分数:")
    var score:Int = StdIn.readInt()

    if (score == 100){
      println("奖励30")
    } else if (score >= 80) {
      println("奖励10w")
    } else if (score >= 60){
      println("奖励2w")
    } else {
      println("没有奖励")
    }
  }
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/chen18677338530/article/details/91440145