slaca是一种近似java和python的语言
在这里输出一个 *
号的直角三角形来对代码进行对比。
下图是在linux中执行的命令
下图是 python
package net.sherry.day01
class HelloWorld {
def speak(): Unit={
println("Hello Scala world")
}
}
package net.sherry.day01
object TestHelloWorld {
def main(args: Array[String]): Unit = {
val hello = new HelloWorld() // 创建对象
hello.speak() //调用方法
}
}
package net.sherry.day01
object ifelse {
def main(args: Array[String]): Unit = {
val age = 20
val result = if (age >= 18){
"OK!"
}else {
if (age>=12){
"Wait!"
}else{
"No way!"
}
}
println(result)
}
}
package net.sherry.day01
object PrintStudentInfo {
def main(args: Array[String]): Unit = {
println("学号:21212121")
println("姓名:sherry")
println("性别:男")
println("专业:大数据技术")
println("兴趣:旅游、音乐、美食")
println("梦想:来一场说走就走的旅行")
}
}
package net.sherry.day02
import scala.io.StdIn
object Example01 {
// def main(args: Array[String]): Unit = {
// print("n = ")
// val n = StdIn.readLine.toInt
// if (n % 2 == 0)
// println(n.toString + "是偶数")
// else
// println(n.toString + "是奇数")
// }
def main(args: Array[String]): Unit = {
print("n = ")
val n = StdIn.readLine.toInt
val result = if (n % 2 == 0)
n.toString + "是偶数~"
else
n.toString + "是奇数~"
println(result)
}
}
package net.sherry.day02
/**
* 功能:双重循环
* 作者:Sherry
* 日期:2023年02月22日
*/
object Example02 {
def main(args: Array[String]): Unit = {
for (i <- 1 to 9){
for (j <- 1 to i){
print(i.toString + "*" + j.toString + "=" + (i*j) + "\t")
}
println()
}
}
}
package net.sherry.day02
object Example03 {
def main(args: Array[String]): Unit = {
for (i <- 1 to 9; j <- 1 to i) {
print(i.toString + "*" + j.toString + "=" + (i * j) + "\t")
if (i == j) println()
}
}
}
package net.sherry.day02
object Example04 {
def main(args: Array[String]): Unit = {
for (i <- 1 to 9; j <- 1 to i; sep = if (j == i) "\n" else "\t"){
print(i.toString + "*" + j + "=" + (i*j) + sep)
}
}
}
package net.sherry.day02
object Example05 {
def main(args: Array[String]): Unit = {
val list = for (i <- 1 to 9; j <- 1 to i; sep = if (j == i) "\n" else "\t")
yield i.toString + "*" + j.toString + "=" + (i * j) + sep
list.foreach(print)
}
}
package net.sherry.day02
object Example06 {
def main(args: Array[String]): Unit = {
for (i <- 1 to 3; j <- 1 to 3; sep = if (j == 3) "\n" else "\t") {
if (i == j) {
print("" + sep)
}
else{
print((i * j).toString + sep)
}
}
}
}
package net.sherry.day02
object Example07 {
def main(args: Array[String]): Unit = {
for (i <- 1 to 3){
for (j <- 1 to 3){
if (i ==3){
println()
}
else {
print((i*j).toString)
}
}
}
}
}
package net.sherry.day02
object Example08 {
def main(args: Array[String]): Unit = {
var count = 0
for (i <- 0 to 20) {
for (j <- 0 to 33) {
for (k <- 0 to 300) {
if (i + k + j == 100 && i * 5 + j * 3 + k/3 == 100) {
count += 1
println("公鸡:" + i + "只," + "母鸡:" + j + "只," + "小鸡:" + k + "只")
}
}
}
}
println("总共有" + count + "中购买方式!")
}
}
package net.sherry.day02
object Example09 {
def main(args: Array[String]): Unit = {
var sum = 0
var i = 1
while (i<=100){
sum += i
i += 1
}
println("1+2+3+……+100=" + sum)
}
}
package net.sherry.day02
object Example10 {
def mx(n: Int, sum: Int): Int = {
if (n > 0) mx(n - 1, sum + n) else sum
}
def main(args: Array[String]): Unit = {
println("1+2+3+……+100=" + mx(100,0))
}
}
package net.sherry.day02
object Example11 {
def main(args: Array[String]): Unit = {
for (n <- 100 to 999){
var sum = 0
val num = n.toString
for (i <- 0 until num.length){
// println(i)
// println(num(i))
sum += (num(i).toInt-48)*(num(i).toInt-48)*(num(i).toInt-48)
// println(n)
// println(sum)
}
if (n == sum) {
println(n.toString)
}
}
}
}
package net.sherry.day02
object Example12 {
def main(args: Array[String]): Unit = {
var sum = 0
var i = 1
do {
sum += i
i +=1
}while (i <= 100)
println("1+2+3+……+100 = " + sum)
}
}
package net.sherry.day02
import java.io.IOException
object Example13 {
def main(args: Array[String]): Unit = {
var message = ""
val result = try {
mx() //调用方法去抛出异常
"执行成功!"
} catch {
case e: NullPointerException => "空指针异常"
case e: IOException => "呵呵,I/O异常咯"
case e: RuntimeException => "运行时候异常"
case e: Exception => "反正是异常"
} finally {
message = "程序到此为止!"
"无论是否有异常都会执行 finally 里的语句"
}
println(message)
println(result)
}
def mx(): Unit = {
throw new RuntimeException("故意抛出运行时异常~")
}
}
package net.sherry.day02
import scala.io.StdIn
object Example14 {
def main(args: Array[String]): Unit = {
print("输入城市:")
val city = StdIn.readLine()
val comment = city match {
case "北京" => "最伟大的首都"
case "上海" => "最神奇的魔都"
case "雅安" => "四面环山的城市"
}
println(city + comment)
}
}
package net.sherry.day02
object forstr {
def main(args: Array[String]): Unit = {
val a = "internationalization"
a.indices.foreach(print)
// print(Character.isDigit("a"))
}
}
package net.sherry.day05
class Cat private (var name: String, var age: Int){
def speak(): Unit = println("我叫" + name + ",今年" + age + "岁了~")
}
object Cat {
def main(args: Array[String]): Unit = {
val cat = new Cat("加菲", 2)
cat.speak()
}
}
package net.sherry.day05
class Dog (var name : String, var age: Int){
name = "大黄"
age = 5
println("主构造方法调用")
def speak(): Unit = println("我叫" + name + ",今年" + age + "岁了~")
}
object Dog{
def main(args: Array[String]): Unit = {
val dog = new Dog("", 0)
dog.speak()
}
}
package net.sherry.day05
// 伴生类
class Person (val name : String, var age: Int = 18) { // 主构造器两个参数
def speak():Unit = println("我叫" + name + ",今年" + age + "岁了~")
}
// 伴生对象
object Person {
def main(args: Array[String]): Unit = {
// 基于主构造器创建对象,传入两个参数值
val person = new Person("余淼")
// 访问构造器参数,其实是访问成员属性
println("姓名:" + person.name)
println("年龄:" + person.age)
// 调用对象的方法
person.speak()
// 修改对象属性(name不能改, age可以改)
person.age = (21)
// 再次调用对象方法
person.speak()
}
}
package net.sherry.day05
class Student {
private var name = "老余"
private var age = 18
// 定义单参辅助构造器
def this(name: String) = {
this() // 调用无惨主构造器
this.name = name
}
def this(name: String, age: Int)={
this(name) // 调用单参辅助
this.age = age
}
// 重写 toString 方法
override def toString: String = "我叫" + name + ",今年" + age + "岁了~"
}
object Student {
def main(args: Array[String]): Unit = {
// 调用三参辅助构造器实例化
val student1 = new Student()
println(student1)
// 调用三参辅助构造器实例化
val student2 = new Student("王晓林")
println(student2)
// 调用三参辅助构造器实例化
val student3 = new Student("张智霖", 21)
println(student3)
}
}
package net.sherry.day05
/**
* 功能:教师类
* 作者:Sherry
* 日期:2023年03月22日
*/
class Teacher (private var name: String, private var age: Int){ // 双参主构造器
private var gender = ""
// 三参辅助构造器
def this(name: String, age: Int, gender: String) = {
this(name, age) // 调用双参主构造器
this.gender = gender
}
// 重写toString方法
override def toString: String = "我叫" + name + "," + gender + ",今年" + age + "岁了~"
}
// 伴生对象
object Teacher {
def main(args: Array[String]): Unit = {
// 调用三参辅助构造器实例化
val teacher = new Teacher("Sherry", 20, "男")
println(teacher)
}
}
package net.sherry.day06
/**
* 功能:抽象人类
* 作者:sherry
* 日期:2023年03月16日
*/
abstract class Person {
var name: String // 抽象字段
var age: Int // 抽象字段
var address: String = "雅安市雨城区县前街88号" // 普通字段
// 抽象方法
def speak()
// 普通方法
def walk(): Unit = {
println(name + "在散步~")
}
}
package net.sherry.day06
class Teacher extends Person {
var name: String = "余淼"
var age: Int = 20
// 实现抽象方法,不用家 override
def speak(): Unit = {
println("我叫" + name + ",今年" + age + "岁,家住" + address + ",擅长吃喝玩乐~" )
}
// 重写普通方法,必须加override
override def walk(): Unit = {
println("教师" + name + "喜欢雨中漫步~")
}
}
package net.sherry.day06
object TestTeacher {
def main(args: Array[String]): Unit = {
val teacher= new Teacher()
//调用
teacher.speak()
teacher.walk()
}
}
package net.sherry.day07
class Bird extends Pet with Runnable with Flyable {
var name: String = "杜鹃"
var age: Int = 5
def speak: Unit = {
println("我叫" + name + ",今年" + age + "岁了~")
}
def run: Unit = {
println("鸟儿[" + name + "]在欢快地奔跑~")
}
def fly: Unit = {
println("鸟儿[" + name + "]在自由地飞翔~")
}
}
// 伴生对象
object Bird {
def main(args: Array[String]): Unit = {
// 创建鸟对象
val bird = new Bird()
// 调用对象方法
bird.speak
bird.run
bird.fly
}
}
package net.sherry.day07
/**
* 功能:猫类
* 作者:华卫
* 日期:2023年03月16日
*/
class Cat extends Pet {
// 实现抽象字段
var name: String = "虎丸"
var age: Int = 3
// 实现抽象方法
def speak: Unit = {
println("我叫" + name + ",今年" + age + "岁了~")
}
// 重写普通方法
override def eat: Unit = {
println(name + "在吃鱼虾~")
}
}
// 伴生对象
object Cat {
def main(args: Array[String]): Unit = {
// 创建猫对象
val cat = new Cat()
// 调用对象方法
cat.speak
cat.eat
}
}
package net.sherry.day07
trait Flyable {
def fly
}
package net.sherry.day07
/**
* 功能:宠物特质
* 作者:华卫
* 日期:2023年03月16日
*/
trait Pet {
// 抽象字段
var name: String
var age: Int
// 抽象方法
def speak
// 普通方法
def eat: Unit = {
print(name + "在吃东西~")
}
}
package net.sherry.day07
trait Runnable {
// 抽象方法
def run
}