当前位置:网站首页>基本语法(一)
基本语法(一)
2022-07-31 12:16:00 【Hanyang Li】
1. String 语句
//String 语句比较
fun main(args: Array<String>) {
var str1 = "Andy"
var str2 = "andy"
println(str1 == str2)
println(str1.equals(str2))
//ignoreCase 是否忽略大小写
println(str1.equals(str2,true))
}2. when 语句
//10分满分 9分干的不错 8分还可以 7还需要努力6分刚好及格 其他(需要加油)
fun gradeStudent(score: Int) {
when (score) {
10 -> println("满分")
9 -> println("干的不错")
8 -> println("还可以")
7 -> println("还需要努力")
6 -> println("刚高及格")
else -> println("需要加油")
}
}
//测试
fun main(args: Array<String>) {
gradeStudent(9)
gradeStudent(3)
}
3. when 语句加返回值
fun diaryGenerator(placeName: String): String{
var diary = """今天天气晴朗,万里无云,我们去${placeName}玩,首先看到的是$placeName${numToChinese(placeName.length)}个大字 """
return diary
}
fun numToChinese(num: Int):String{
var result = when(num){
1 -> "一"
2 -> "二"
3 -> "三"
4 -> "四"
5 -> "五"
6 -> "六"
7 -> "七"
else -> "地名太长,记不清了"
}
return result
}
//测试
fun main(args: Array<String>){
println(diaryGenerator("小山村"))
}4. Range和Loop for循环与区间
//until 不包换 100
fun demo1(){
var nums = 1 until 100
for (num in nums){
println(num)
}
}
// 区间 1 至 16 步长为2 reversed 反转
fun demo2(){
var nums = 1 .. 16
for (num in nums step 2){
println(num)
}
var nums2 = nums.reversed()
for (num2 in nums2){
println(num2)
}
println(nums2.count())
}边栏推荐
- imx6ull看门狗使用
- SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
- 串的基本概念与操作
- 【Shader】Shader官方示例[通俗易懂]
- JVS低代码能力简介及功能清单
- Qt鼠标穿透
- 0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
- The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!
- The function of SQL GROUP BY dependence
- A Week of Wonderful Content Sharing (Issue 14)
猜你喜欢

Character Functions and String Functions

Data Persistence Technology - MP

Power BI----几个常用的分析方法和相适应的视觉对象

JVS低代码能力简介及功能清单

Is the working process of the belt you know the story - actionreducerstore

kubernetes之服务发现

Markdown编辑器语法

【OpenCV】-边缘检测汇总示例

ESP8266-Arduino编程实例-MCP9808数字温度传感器驱动

Use docker to build mysql master-slave
随机推荐
Json和对象之间转换的封装(Gson)
TOGAF10标准读书会第2场活动精彩继续,高光时刻回顾!
Wearing detection and action recognition of protective gear based on pose estimation
线性表的基本概念
am335x 看门狗驱动&看门狗应用例程序
最长算术(暑假每日一题 11)
Cognitive-exercise rehabilitation medical robot application design
连续变量离散化教程
Docker practical experience: Deploy mysql8 master-slave replication on Docker
After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?
vivado里那些看不懂的原语
Structural controllability of switched linear systems with symmetry constraints
CameraToolUnity中两种摄像机的两种观察控制方式
PAT exam summary (exam experience)
Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql
WPF中报错:“未将对象引用设置到对象的实例。”
荣耀手机参数写错,客服认为没错
0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
最近两个月谷歌 ad 掉的厉害