Scanner接收用户输入的数据:----------背下来,不需要理解
在package下:
import java.util.Scanner;
在main中:
Scanner scan = new Scanner(System.in);
在第2步之下:
System.out.println("请输入年龄:");
int age = scan.nextInt();
System.out.println("请输入价格:");
double price = scan.nextDouble();
分支结构
- if结构:1条路
- if…else结构:2条路
- if…else if结构:多条路
- switch…case结构:多条路
优点:效率高、结构清晰
缺点:只能对整数判断相等
break:跳出switch
循环结构
反复多次执行一段相同或相似的代码
三要素
- 循环变量的初始化
- 循环的条件(以循环变量为基础)
- 循环变量的改变(向着循环的结束变)
循环变量:在整个循环过程中所反复改变的那个数
写法:
- while:先判断后执行,有可能一次都不执行
- do…while:先执行后判断,至少执行一次
要素1与要素3相同时,首选do…while
总结
- 顺序结构:从上往下逐行执行,每句必走
- 分支结构:有条件的执行某语句一次,并非每句必走
- 循环结构:有条件的执行某语句多次,并非每句必走
if (boolean) { // 只判断一次
语句块 // 最多只执行一次
}
while (boolean) { // 判断多次
语句块 // 执行多次
}
易错、问题:
- case后的数不能相同
- case一个数字后是:而不是;
int num = 5;
switch (num) {
case 1;
System.out.println(111);
case 2;
System.out.println(222);
case 1;
System.out.println(333);
default:
System.out.println(666);
}
- 循环变量必须得初始化之后才能使用
int times;
while (times < 10) {
System.out.println("行动是成功的阶梯");
times++;
}
- 缺少第3要素,此时程序运行结果应该是死循环
int times = 0;
while (times < 10) {
System.out.println("行动是成功的阶梯");
}
- 在while中的guess编译错误,因为变量guess的作用范围仅在do中,超出范围就错
do {
System.out.println("猜吧!");
int guess = scan.nextInt();
} while (guess!=num); // 编译错误
- 在while条件的后面,缺少了;号
do {
System.out.println("猜吧!");
int guess = scan.nextInt();
} while(guess != num)
代码部分:
ScannerDemo.java
package day04;
import java.util.Scanner; // 1.
/**
* Scanner的演示
* @author Katrina
*/
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // 2.
System.out.println("请输入年龄:");
int age = scan.nextInt(); // 3.
System.out.println("请输入价格:");
double price = scan.nextDouble();
System.out.println(age);
System.out.println(price);
}
}
IfElseIfDemo.java
package day04;
/**
* if...else if结构的演示
* @author Katrina
*/
public class IfElseIfDemo {
public static void main(String[] args) {
/*
* 3.if...else if结构: 多条路
* 1)语法:
* if (boolean-1) {
* 语句块1
* } else if (boolean-2) {
* 语句块2
* } else if (boolean-3) {
* 语句块3
* } else {
* 语句块4
* }
* 2)执行过程:
* 判断boolean-1,若为true则执行语句块1(结束),若为false则
* 再判断boolean-2,若为true则执行语句块2(结束),若为false则
* 再判断boolean-3,若为true则执行语句块3(结束),若为false则执行语句块4(结束)
* 3)结论: 语句块1/2/3/4,只执行其中之一
*/
}
}
ScoreLevel.java
package day04;
import java.util.Scanner;
/**
* 成绩等级判断
* @author Katrina
*/
public class ScoreLevel {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = scan.nextDouble();
// 888, -78, 95, 85, 65, 45
if (score < 0 || score > 100) { // 不合法
System.out.println("成绩不合法");
} else if (score >= 90) { // 合法
System.out.println("A-优秀");
} else if (score >= 80) {
System.out.println("B-良好");
} else if (score >= 60) {
System.out.println("C-中等");
} else {
System.out.println("D-不及格");
}
}
}
SwitchCaseDemo.java
package day04;
/**
* switch...case的演示
* @author Katrina
*/
public class SwitchCaseDemo {
public static void main(String[] args) {
int num = 2;
switch (num) { // byte, short, int, char, String(JDK1.7开始支持)
case 1: // if (num == 1)
System.out.println(111);
break;
case 2: // 以此为入口
System.out.println(222);
break; // 跳出switch
case 3:
System.out.println(333);
break;
default: // 所有case都未匹配时执行
System.out.println(666);
}
}
}
CommandBySwitch.java
package day04;
import java.util.Scanner;
/**
* 命令解析程序
* @author Katrina
*/
public class CommandBySwitch {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请选择功能: 1.查询余额 2.存款 3.取款 4.退卡 ");
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 4:
System.out.println("下次再来吧!");
break;
default:
System.out.println("输入错误");
}
}
}
WhileDemo.java
package day04;
/**
* while结构的演示
* @author Katrina
*/
public class WhileDemo {
public static void main(String[] args) {
/*
int num = 1;
while (num <= 9) {
System.out.println(num + "*9=" + num * 9);
num += 2; // num++;
}
System.out.println("over");
*/
/* 执行过程:
* num=1
* true 1*9=9 num=3
* true 3*9=27 num=5
* true 5*9=45 num=7
* true 7*9=63 num=9
* true 9*9=81 num=11
* false while循环结束
* 输出over
*/
/*
int times = 0; // 1)循环变量的初始化
while (times < 10) { // 2)循环的条件
System.out.println("行动是成功的阶梯");
times++; // 3)循环变量的改变
}
System.out.println("over");
*/
/* 执行过程:
* times=0
* true 输出 times=1
* true 输出 times=2
* true 输出 times=3
* ... times=4/5/6/7/8
* true 输出 times=9
* true 输出 times=10
* false while循环结束
* 输出over
*/
/*
* 1.while结构:
* 1)语法:
* while (boolean) {
* 语句块/循环体--------反复执行的代码
* }
* 2)执行过程:
* 判断boolean的值,若为true则执行语句块,
* 再判断boolean的值,若为true则再执行语句块,
* 再判断boolean的值,若为true则再执行语句块,
* 如此反复,直到boolean为false时循环结束
*/
}
}
DoWhileDemo.java
package day04;
/**
* do...while结构的演示
* @author Katrina
*/
public class DoWhileDemo {
public static void main(String[] args) {
/*
* 2.do...while结构:
* 1)语法:
* do {
* 语句块/循环体
* } while (boolean);
* 2)执行过程:
* 先执行语句块,再判断boolean的值,若为true则
* 再执行语句块,再判断boolean的值,若为true则
* 再执行语句块,再判断boolean的值,
* 如此反复,直到boolean的值为 false时循环结束
*/
}
}
Guessing.java
package day04;
import java.util.Scanner;
/**
* 猜数字小游戏
* @author Katrina
*/
public class Guessing {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int) (Math.random() * 1000 + 1); // 1到1000之内的随机数
System.out.println(num); // 作弊
// num = 867 900(大), 800(小), 867(对)
int guess;
do {
System.out.println("猜吧!");
guess = scan.nextInt(); // 1 + 3
if (guess > num) {
System.out.println("太大了");
} else if (guess < num) {
System.out.println("太小了");
} else {
System.out.println("恭喜你猜对了");
}
} while (guess != num); // 2
/*
// 300(大),200(小),250(恭喜你)
System.out.println("猜吧!");
int guess = scan.nextInt(); // 1.
while (guess != num) { // 2.
if (guess > num) {
System.out.println("太大了");
} else {
System.out.println("太小了");
}
System.out.println("猜吧!");
guess = scan.nextInt(); // 3.
}
System.out.println("恭喜你猜对了!");
*/
}
}