一、配置相关环境
1.增加项目
在idea里面创建新的maven项目
2. 在pom文件中增加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>bigdatascala</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>scala</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies>
<--scala语言的三个包--> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.12</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-compiler</artifactId> <version>2.11.12</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-reflect</artifactId> <version>2.11.12</version> </dependency> <--导入MySQL的依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <--解析json工具--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency> </dependencies> <build> <plugins> <!-- Java Compiler --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Scala Compiler --> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
二、Scala 用法 (变量、字符串、拼接、循环、数组)
/* def: scala中定义方法的关键字 main: 方法名 args:Array[String]方法的参数 args:参数名 Array[String] 参数类型 String : 泛型 Unit: 相当于Java中的void */ //object 理解为单例对象,相当于Java中的给方法增加static静态关键字 object Demo1 { def main(args: Array[String]): Unit = { println("hello") //变量 val a: String = "0" val b = 13 val flag = true var c = 77 c = 78 println(a.getClass) println(flag.getClass) println(a, b, c) //字符串 val value: String = "string,block,var,char,int,long,double,long,float,boolean" println(value.mkString(" ")) val s3: Array[String] = value.split(",") //增强for 循环遍历数组 for (elem <- s3) { print(elem) print("\t") } println(" ^*^ " * 30) val str: String = "java,hadoop,spark" val s2: Array[String] = str.split(",") println(s2.mkString("|")) println(" ^*^ " * 20) //字符串拼接 val w = "我" + "是" + "最棒" + "的" println(w) val builder = new StringBuilder builder.append("java") builder.append("java1") builder.append("java2") builder.append("java3") println(builder) val str2 = s"$a,$b,$c,$flag" println(str2) println("---+++" * 30) //数组 val ints: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) println(ints(5)) println(ints mkString ".") ints(5) = 55 println("-----" * 10) for (t <- ints) { print(t) print("|") } println("\r\n") //if else 条件语句 val scanner = new Scanner(System.in) print("请输入你要输入的年龄:") val i: Int = scanner.nextInt() if (i >= 18) { println("您已成年!") } else { println("对不起您未成年!!禁止未成年人进入网吧!请您成年后再过来") } //循环 val array: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) var j = 0 while (j < array.length) { println(array(j)) j += 1 } println(".............") for (elem <- array) { println(elem) } // } }