1.Scanner(扫描器)对象
java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入。
下面是创建 Scanner 对象的基本语法:
import java.util.Scanner;
Scanner s = new Scanner(System.in);

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.util.Scanner;
public class Demo{ public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.println("使用next方式接受:"); if (s.hasNext()){ String str = s.next(); System.out.println("输入的内容是:"+str); } s.close(); } }
hello之前的空格会过滤
public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.println("使用nextLine方式接受:"); if (s.hasNextLine()){ String str = s.nextLine(); System.out.println("输入的内容是:"+str); } s.close(); }
hello之前的空格会接收
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Demo{ public static void main(String[] args){ Scanner s = new Scanner(System.in); double sum = 0; int sign = 0; while (s.hasNextDouble()){ sum += s.nextDouble(); sign ++; System.out.println("你输入了第"+sign+"个数据,结果是"+sum); } System.out.println(sign+"个数的总和是"+sum+",平均值是"+sum/sign); s.close(); } }
|
2.选择分支
if选择分支
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Demo{ public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.println("请输入内容:"); String str = s.nextLine(); if(s.equals("hello")){ System.out.println(str); } System.out.println("End!"); s.close(); } }
|

switch选择分支

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Demo{ public static void main(String[] args){ char grade = 'F'; switch (grade){ case 'A': System.out.println("优秀");break; case 'B': System.out.println("良好");break; case 'C': System.out.println("挂科");break; default: System.out.println("未知"); } } }
|
3.循环结构
包括while,do-while和for循环,break和continue跳出循环。大致语法结构与C++相同。
在JDK5中引入了一种主要用于数组的增强型for循环。
关于goto,它是java的保留字,但是并没有得到正式使用。


1 2 3 4 5 6 7 8 9
| public class Demo{ public static void main(String[] args){ int[] nums = {1,2,3,4,5,6}; for (int x:nums){ System.out.println(x); } } }
|
4.Java中的方法

1 2 3 4 5 6 7 8 9
| public class Demo{ public static void main(String[] args){ int sum = add(1,2); System.out.println(sum); } public static int add(int a,int b){ return a+b; } }
|


记住:Java中的方法都是值传递。其实 Java 只存在一种传参的方式,就是用实参给形参赋值。
5.方法的重载(非常重要)

方法重载基本类似C++中的函数重载。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Demo{ public static void main(String[] args){ int sum1 = add(1,2); double sum2 = add(1.0,2.0); double sum3 = add(1,2.0); System.out.println(sum1); System.out.println(sum2); System.out.println(sum3); } public static int add(int a,int b){ return a+b; } public static double add(double a,double b){ return a+b; } }
|

重载的特殊之处在于:如果参数满足自动类型转换的方法有好几个,重载规则是选择最近的调用。
比如:int add(int a)和int add(double b),如果传入byte,short,int类型的参数a,会调用第一个,传入float,double类型的参数a会调用第二个。自己写的话最好避免这样的情况。
6.给main函数传参
6.1通过命令行传参

上图中String args[]
意思同String[] args
,前者是类C的写法,后者是Java写法。

注意:要通过java命令运行放在package中的class文件,必须回退到包的路径(此处是src目录)。
通过java命令给main方法传递参数”this is…”,并打印参数。
6.2通过IDEA传参


1 2 3 4 5 6
| 输出: 4 anb 123 qw sd df 王小明
|
7.可变参数(不定项参数)

1 2 3 4 5 6 7 8 9
| public class Demo{ public static void main(String[] args){ Demo demo = new Demo(); demo.test(1,2,3); } public void test(int... i){ System.out.println(i[1]); } }
|
8.数组
数组是引用数据类型。数组的长度是固定的,一旦创建就不能修改。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Demo{ public static void main(String[] args){ int[] num1; num1 = new int[10]; int num2[]; int[] num3 = new int[12]; for (int i = 0;i < num1.length;i ++){ num1[i] = i+1; } for(int x: num1){ System.out.println(x); } } }
|

1 2 3 4 5 6 7 8 9
| public class Demo{ public static void main(String[] args){ int[] a = {1,2,3,4,5,6,7,8,9,0}; int[] b = new int[10]; b[0] = 10; } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Demo{ public static void main(String[] args){ int[] arrays = {1,2,3,4,5}; int[] result = reverse(arrays); printArray(result); } public static void printArray(int[] arrays){ for (int array:arrays){ System.out.println(array); } } public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; for (int i = 0,j = result.length-1;i < arrays.length;i++,j--){ result[j] = arrays[i]; } return result; } }
|
多维数组
1 2 3 4 5 6
| public class Demo{ public static void main(String[] args){ int[][] array = {{1,2},{2,3},{3,4},{4,5}}; System.out.println(array[0][1]); } }
|
9.内存分析

数组的内存机制:
1.声明数组array,存放于栈中;
2.创建数组array,在堆中开辟空间。(数组对象本身是在堆中的)

10.Arrays类

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.util.Arrays;
public class Demo{ public static void main(String[] args){ int[] a = {1,5,67,345,2,456,67,45}; System.out.println(Arrays.toString(a)); Arrays.sort(a); System.out.println(Arrays.toString(a)); Arrays.fill(a, 0); System.out.println(Arrays.toString(a)); Arrays.fill(a, 2,4,1); System.out.println(Arrays.toString(a)); } }
|