1、Scanner(接收用户输入的数据)
1)共三步:
--------------①import java.util.Scanner;【在public class上一行】
--------------②Scanner scan = new Scanner(System.in); 【在main中】
--------------③int command = scan.nextInt();【在main中】
2)暂时不需要理解
2、分支结构(下)
1)if...else if:多条路
------①语法:
if(boolean-1){
语句块1
}else if(boolean-2){
语句块2
}else if(boolean-3){
语句块3
}else{
语句块4
}
------②执行过程:
判断boolean-1,若为true则执行语句块1(结束),若为false则
再判断boolean-2,若为true则执行语句块2(结束),若为false则
再判断boolean-3,若为true则执行语句块3(结束),若为false则执行语句块4(结束)
2)说明:
------语句块1/2/3/4,必走其中之一-------------多选1
3)switch case:多条路
------①优点:效率高,结构清晰
------②缺点:只能对整数进行“相等”判断
------③break:跳出switch
------④语法:
switch(int){
case 1: //int a=1时
System.out.println(111);
break;
case 2: //int a=2时
System.out.println("222");
break;
case 3: //int a=3时
System.out.println("333");
break;
default: //当case1/2/3都不满足时,执行这段程序
System.out.println("666");
}
说明:switch能作用在byte,short,char,int,String,枚举
3、循环结构(上)
1)循环:反复多次执行一段或者相似的代码
2)循环三要素:
------①循环变量的初始化
------②循环的条件(以循环条件为基础)
------③循环变量的改变(向着循环结束的方向改变)
说明:循环变量是在循环过程中反复改变的那个数
3)while:先判断后执行,有可能一次都不执行
------①)语法:
while(boolean){
语句块/循环体------------反复执行的代码
}
------②执行过程:
先判断boolean的值,若为true则执行语句块,
再判断boolean的值,若为true则再执行语句块,
再判断boolean的值,若为true则再执行语句块, 如此反复,直到boolean的值为false时,while循环结束
------③三要素必不可少
------④先输出,再改变循环变量。
4)do...while:先执行后判断,至少执行一次(第一要素和第三要素的代码相同时)
------①语法:
do{ 语句块-------------------反复执行的代码
}while(boolean);
------执行过程:
先执行语句块,再判断boolean的值,若为true则
再执行语句块,再判断boolean的值,若为true则
再执行语句块,如此反复,直到boolean的值为false时,循环结束
4、补充
1)给变量赋值:
------①赋一个固定的值:
int a = 5;
------②接收用户输入的数据:----------Scanner
int a = scan.nextInt();
------③系统随机生成的值:------------Math.random()
int a =(int)( Math.random()*1000+1) ;//1到1000的随机值
2) 任何复杂的业务逻辑都可以通过三种结构来实现:
顺序结构:从上往下逐行执行,每句必走
分支结构:有条件的执行某语句一次,并非每句必走
循环结构:有条件的执行某语句多次,并非每句必走
3)生成随机数:1到1000
Math.random()--------------0.0到0.9999999999999...
*1000----------------------0.0到999.99999999999...
+1-------------------------1.0到1000.9999999999...
(int)----------------------1到1000
4) 变量的作用域/范围: 从变量的声明开始,到包含它最近的大括号结束
package test;
import java.util.Scanner;
//成绩等级判断:if...else if 案例
public class ClassWord {
public static void main(String[] args) {
scanner scan= new Scanner(System.in);
System.out.println("请输入成绩");
double score=scan.nextDouble();
if(score<0 || score>100){
System.out.println("成绩不合法");
}else if(score>90){
System.out.println("成绩优秀A");
}else if(score){
System.out.println("成绩良好B");
}else if(score>60){
System.out.println("成绩合格C");
}else {
System.out.println("成绩不合格D");
}
}
}
package test;
import java.util.Scanner;
public class ClassWork {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//命令解析程序:switc...case案例
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("输入错误");
}
package test;
import java.util.Scanner;
public class ClassWork {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//输出5次“行动是成功的阶梯” int times=0 times<5 times++
int times=0;
while(times<5){
System.out.println("行动是成功的阶梯");
times++;
}
System.out.println("继续执行程序...");
package test;
import java.util.Scanner;
public class ClassWork {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//数字小游戏:输出正确的值,输入的值不对时给出大小提示,直到用户输入正确的数字;while(){}案例
int num=(int)(Math.random()*1000+1);//声明并初始化一个随机数,范围在1到1000
System.out.println(num);//作者去看数字等于多少,作弊器
System.out.println("请猜数字是多少:");
int guess= scan.nextInt();//用户输入数字
while(guess!=num){
// if (guess>250){ 程序错误
if (guess>num){
System.out.println("太大了!");
}else{
System.out.println("太小了!");
}
System.out.println("请猜:");
guess=scan.nextInt();
}
System.out.println("恭喜你,猜对了!!!");
package test;
import java.util.Scanner;
public class ClassWork {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//数字小游戏:输出正确的值,输入的值不对时给出大小提示,直到用户输入正确的数字;do...while(){}案例
int num=(int)(Math.random()*1000+1);//声明并初始化一个随机数,范围在1到1000
System.out.println(num);//作者去看数字等于多少,作弊器
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(num!=guess);