当前位置:网站首页>One of scala variables and data types
One of scala variables and data types
2022-07-23 11:42:00 【AA master Zhao】
1、 String output
(1) character string , adopt + Connection No
(2)printf usage : character string , adopt % Pass value .
(3) String template ( Interpolation string ): adopt $ Get the value of the variable
package com.atguigu.chapter02
object TestCharType {
def main(args: Array[String]): Unit = {
var name: String = "jinlian"
var age: Int = 18
//(1) character string , adopt + Connection No
println(name + " " + age)
//(2)printf Usage string , adopt % Pass value .
printf("name=%s age=%d\n", name, age)
//(3) character string , adopt $ quote
// Multiline string , stay Scala in , You can use three double quotes to surround multiple lines of string .// Input content , With spaces 、\t And so on , As a result, the beginning of each line cannot be neatly aligned .
// application scala Of stripMargin Method , stay scala in stripMargin The default is “|” As a connector ,// Add a... Before the line header of multi line wrap “|” A symbol is enough .
val s =
"""
|select
| name,
| age
|from user
|where name="zhangsan"
""".stripMargin
println(s)
// If you need to operate on variables , Then you can add ${}
val s1 =
s"""
|select
| name,
| age
|from user
|where name="$name" and age=${age+2}
""".stripMargin
println(s1)
val s2 = s"name=$name"
println(s2)
}
}
2、 Keyboard entry
In programming , Need to receive the data input by the user , You can use keyboard input statements to get .
1) Basic grammar
StdIn.readLine()、StdIn.readShort()、StdIn.readDouble()
2) Case practice
demand : Can receive user information from the console ,【 full name , Age , salary 】.
import scala.io.StdIn
object TestInput {
def main(args: Array[String]): Unit = {
// 1 Enter a name
println("input name:")
var name = StdIn.readLine()
// 2 Enter the age
println("input age:")
var age = StdIn.readShort()
// 3 Enter salary
println("input sal:")
var sal = StdIn.readDouble()
// 4 Print
println("name=" + name)
println("age=" + age)
println("sal=" + sal)
}
}
3、 Type conversion
Cast
The reverse process of automatic type conversion , Convert a numeric type with high precision to a numeric type with low precision . When you use it, you need to add the forced conversion function , But it may cause accuracy reduction or overflow , Pay special attention to
Java : int num = (int)2.5
Scala : var num : Int = 2.7.toInt
(1) Send the data from High precision Convert to Low precision , You need to use to cast
(2) The strong rotation symbol is only valid for the nearest operand , Curly braces are often used to increase priority
object TestForceTransfer {
def main(args: Array[String]): Unit = {
//(1) Convert data from high precision to low precision , You need to use to cast
var n1: Int = 2.5.toInt // There is a loss of accuracy in this
//(2) The strong rotation symbol is only valid for the nearest operand , Curly braces are often used to increase priority
var r1: Int = 10 * 3.5.toInt + 6 * 1.5.toInt // 10 *3 + 6*1 = 36
var r2: Int = (10 * 3.5 + 6 * 1.5).toInt // 44.0.toInt = 44
println("r1=" + r1 + " r2=" + r2)
}
}
Numerical types and String Conversion between types
1) Basic type rotation String type ( grammar : Put the value of the basic type +"" that will do )
(2)String Type to basic numeric type ( grammar :s1.toInt、s1.toFloat、s1.toDouble、s1.toByte、s1.toLong、s1.toShort)
object TestStringTransfer {
def main(args: Array[String]): Unit = {
//(1) Basic type rotation String type ( grammar : Put the value of the basic type +"" that will do )
var str1 : String = true + ""
var str2 : String = 4.5 + ""
var str3 : String = 100 +""
//(2)String Type to basic numeric type ( grammar : Call the relevant API)
var s1 : String = "12"
var n1 : Byte = s1.toByte
var n2 : Short = s1.toShort
var n3 : Int = s1.toInt
var n4 : Long = s1.toLong
}
}
边栏推荐
- upload-lab第1~4关
- 用户连续登陆(允许中断)查询sql
- General Query & paging code
- xtu-ctf Challenges-Reverse 1、2
- NepCTF2022 Writeup
- Compilation principle - detailed explanation of syntax analysis
- mysql根据中文字段首字母排序
- Yarn容量调度器设置
- CTF web common software installation and environment construction
- Genesis provided a loan of US $2.36 billion to Sanya capital
猜你喜欢
随机推荐
Web component - the lifecycle of a custom element
Web Component-自定義元素的生命周期
Upload lab level 1-4
[pyautogui learning] screen coordinates and mouse scrolling
数字藏品系统开发:百度AI致敬中国航空
NFT trading platform digital collection system | development and customization
数字藏品开发/数字藏品系统开发解决方案
Unable to negotiate with port 51732: no matching host key type found. Their offer:
NFT数字藏品平台开发搭建,源码开发数字藏品
Entrepôt de données 4.0 Notes - acquisition de données commerciales
XML modeling
Custom formula input box
Digital collection development / meta universe digital collection development
mysql免密登录设置
自定义MVC(下)
[uiautomation] key instructions (and three call methods) + common mouse actions +sendkeys+inspect learning
MySQL functions & views & import and export
NepCTF 2022 MISC <签到题>(极限套娃)
The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition - preliminary writeup
Command Execution Vulnerability and defense









