1.Scanner/扫描仪
用来接受用户输入的数据,分三步
package Test;
import java.util.Scanner;//(1)导入一个扫描仪
public class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);// (2)新建一个scan扫描仪
System.out.println("输入一个值");
int a = scan.nextInt();// (2)扫描一个整数并赋值给a
}
}
2.if...else if (多路分支)
结构:
if (boolean1){
代码块1
}else if (boolean2){
代码块2
}else if (boolean3){
代码块3
}else{
代码块4
}
判断boolean-1,若为true则执行语句块1(整个结束),
若为false则再判断boolean-2,若为true则执行语句块2(整个结束),
若为false则再判断boolean-3,若为true则执行语句块3(整个结束),
若为false则执行语句块4(整个 结束)
举例:
public static void main(String[] args) {
int score = 81;
if (score >= 90){
System.out.println("优秀");
}else if (score >= 80){
System.out.println("良好");//符合条件,结果为"良好"
}else if (score >= 60){
System.out.println("中等");
}else{
System.out.println("不及格");
}
}
3.switch...case结构
相比较if...else if效率高、结构清晰,缺点是只能对整数判断相等。
break用于跳出switch
Scanner scan = new Scanner(System.in);
System.out.println("请选择功能:1.话费查询 2.充话费 3.查询余额 0.人工服务");
int command = scan.nextInt();
switch (command){
case 1:
System.out.println("话费查询");
break;
case 2:
System.out.println("充话费");
break;
case 3:
System.out.println("查询余额");
break;
case 0:
System.out.println("人工服务");
break;
default:
System.out.println("输入错误");
}
若业务是对整数判断相等,首选switch。。。case
4、循环:反复多次执行一段相同或类似的代码
循环三要素:
1.循环变量的初始化
2.循环的条件(以循环变量为基础)
3.循环变量的改变(向循环的结束改变)
5、循环结构:
(1).while循环:先判断条件后执行代码,有可能一次都不执行
格式:while(boolean){
代码块-----反复执行
}
代码执行过程:
先判断boolean的值,若为true则执行代码块
再返回判断boolean的值,为true再执行代码块
再返回判断boolean的值,为true再执行代码块
一直反复直到boolean的值为false时,while循环结束
//输出5次"行动是成功的阶梯"
int times = 0; //1.循环变量的初始化
while (times < 5){ //2.循环的条件
System.out.println("行动是成功的阶梯");
times++; //3.循环变量的改变
}
//输出9的乘法表
int num = 1; //1.循环变量的初始化
while (num <= 9){ //2.循环变量的条件
System.out.println(num + "*9="+num * 9);
num++; //3.循环变量的改变
}
(2).do...while循环:先执行后判断,至少会执行一次
格式:
do{
代码块
}while(boolean);
执行过程:
先执行代码块,再判断boolean的值,若为true则再执行代码块,
再判断boolean的值,为true则再执行代码块,
再判断boolean的值,为true则再执行代码块
一直反复,直到boolean的值为false,do...while循环结束
//猜数字游戏
Scanner scan = new Scanner(System.in);
int num =(int) (Math.random()*100);
int guess;
do {
System.out.println("猜数字:");
guess = scan.nextInt();
if (guess > num){
System.out.println("太大了");
}else if (guess < num){
System.out.println("太小了");
}else {
System.out.println("恭喜你猜对了");
}
}while (guess != num);
(3).for循环:
格式:
for(要素1;要素2;要素3){
代码块-----反复执行
}
//输出5次“行动是成功的阶梯”
for (int times = 0;times < 5;times++){
System.out.println("行动是成功的阶梯");
}
//输出9的乘法表
for (int num = 1;num <= 9;num++){
System.out.println(num +"*9="+num*9);
}
for循环中循环变量的作用域仅在当前for中
(4).while、do while、for三种循环如何选择:
循环是否与次数相关
相关----------直接用for
不相关,再看要素1与要素3的代码是否相同
相同-------------直接用do while
不相同----------直接用while
(5).
break:用于跳出循环,可以用在switch和循环中
for (int i = 0; i < 9; i++) {
if (i == 5){//当i等于5时结束循环
break; //跳出循环
}
System.out.println(i + "*9="+i*9);
}
continue:用于跳过循环体中剩余语句进入下一次循环(注意continue只能用在循环中)
for (int i = 0; i <= 9; i++) {
if (i % 5 == 0){//当i取余5等于0时
continue; //跳过循环体中剩余语句进入下一轮循环
}
System.out.println(i + "*9="+i*9);
}
(6).嵌套循环
循环中套循环,常常多行多列时使用,一般外层控制行,内层控制列
执行规则:外层循环走一次,内层循环走所有次
嵌套层数越少越好
break只能跳出当前层循环
//九九乘法表
for (int i = 1; i <= 9; i++) { //控制行
for (int j = 1; j <= i; j++) { //控制列
System.out.print(j+"*"+i+"="+j*i+"t");
}
System.out.println(); //换行
}
6.数组
数组是一种数据类型(应用类型)
相同数据类型元素的集合
(1).格式:
int [] arr = new int [10];
//声明整型数组arr,包含10个元素,每个元素都是int型。默认值为0
int[] arr = new int[10];
int[] arr1 ={1,2,3};
int[] arr2 = new int[]{11,22,33};
int[] arr3;
arr = new int[]{1,2,3,4};
//arr = {1,2,3};编译错误,此方式只能在声明同时初始化时使用
(2).访问:
通过数组名 .length可以获取数组的长度(元素的个数)
int[] arr = new int[10];
System.out.println(arr.length);
//输出arr数组长度为10
通过下标/索引来访问数组中的元素,下标从0开始,即第一个元素的下标为0,往后类推
下标最大到(数组的长度-1)
int[] arr = new int[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3; //arr[3] = 4;//编译错误,arr下标最大到2 System.out.println(arr[0]);//输出下标为0的元素,结果为1 System.out.println(arr[arr.length-1]);//输出最后一个元素
(3).遍历/迭代:从头到尾走一遍
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) { //遍历arr数组
arr[i] = (int)(Math.random()*100); //给数组中每个元素赋值为0到99的随机数
System.out.println(arr[i]); //输出每个元素
}
7.数组复制:System.arraycopy( );
int[] a = {10,20,30,40,50};
int[] b =new int[6];
System.arraycopy(a,1,b,0,5);
//a:源数组
//1:源数组的起始下标
//b:目标数组
//0:目标数组的起始下标
//5:要复制的元素个数
int[] b = Arrays.copyOf( );
int[] a = {10,20,30,40,50};
int[] b = Arrays.copyOf(a,5);
//a:源数组
//b:目标数组
//5:目标数组的长度
数组的扩容:
int[] a = {10,20,30,40,50};
a = Arrays.copyOf(a,a.length+1);//创建了一个a数组长度+1的数组,并将数据复制进去
升序:Arrays.sort( );
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {//遍历arr数组
arr[i] = (int)(Math.random()*100);//生成10个随机数元素
}
Arrays.sort(arr); //对arr数组进行升序排列
8.方法/函数/过程:
(1)方法的定义
//无参无返回值
public static void say() {
System.out.println();
}
//有参无返回值
public static void sayHi(String name,int age){
System.out.println();
}
//无参有返回值
public static double getNum(){
return 3.14159;
}
//有参有返回值
public static int plus(int num1,int num2){
int num = num1 +num2;
return num;
在有返回值的方法中,必须有return
return后数据的类型还必须与返回值类型匹配
(2)方法的调用
调用无返回值的方法,有参传参
调用有返回值的方法
public static void main(String[] args) {
double a = getNum();
System.out.println(a);
int b = plus(5,10);
System.out.println(b);
}



