一。键盘录入
目的:
为了让程序的数据更符合开发的数据
让程序更灵活
使用步骤:
-
导包
在class上面写 import java.util.Scanner;
-
创建扫描器对象
Scanner sc = new Scanner(System.in);
-
通过对象获取输入的数据
int x = sc.nextInt(); //将输入的数据(int类型)存储在变量x中
案例演示1
键盘录入两个整数,对这两个数据求和、求平均值,输出结果
public class Demo1 {
public static void main(String[] args) {
/* int x= 10;
int y=20;
int z = x>y ? x++ : y++;
System.out.println(z);*/
//案例1 获取两个数中的最大值 ?:
int a = 99;
int b = 120;
int z = a>b ? a : b;
System.out.println(z);
}
}
案例演示2
键盘录入两个整数,输出其中最大的值。
import java.util.Scanner; //1.
public class Demo2 {
public static void main(String[] args) {
//用户输入两个整数,求和
Scanner sc = new Scanner(System.in);
System.out.println("请你输入一个数字");
int x = sc.nextInt();//sc扫描器会一直等待你输入int整数,建议在这行代码之前 先提示一句话
System.out.println("输入第二个数字");
int y = sc.nextInt();
//求和
System.out.println("和:"+x*y);
System.out.println("程序结束");
}
}
案例演示3
键盘录入三个整数,分别输出最大值和最小值
mport java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
//案例演示3
//键盘录入三个整数,分别输出最大值和最小值
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数字:");
int x = sc.nextInt();
System.out.println("输入第二个数字");
int y = sc.nextInt();
System.out.println("输入第三个数字");
int z = sc.nextInt();
// ?:
int zuida = (x > y) ? x>z? x:z : y>z? y:z ;
System.out.println(zuida);
}
}
二。流程控制语句
概念:可以控制程序的执行流程
分类:顺序结构、选择结构、循环结构
2.1 选择结构
两种选择结构(也可称为条件语句、判断语句):if语句、switch语句
2.2 if语句
格式一:单分支结构
if(比较表达式) { 语句体; }
执行流程: ▪ 先计算比较表达式的值,看其返回值是true还是false。 ▪ 如果是true,就执行语句体; ▪ 如果是false,就不执行语句体;
注意事项:
比较表达式无论简单还是复杂,结果必须是boolean类型
if语句控制的语句体如果是一条语句,大括号可以省略
如果是多条语句,就不能省略。建议永远不要省略
格式二:双分支结构
if(比较表达式) { 语句体1; }else { 语句体2; }
执行流程: 首先计算比较表达式的值,看其返回值是true还是false。 如果是true,就执行语句体1; 如果是false,就执行else后面的语句体2;
注意事项:
else后面是没有比较表达式的,只有if后面有
案例演示4
判断一个整数是奇数还是偶数,分别用if语句和三元运算符完成。
区别:
if语句控制可以直接使用输出语句
三元运算符是一个运算符,运算符操作后必须返回一个结果,不能直接输出。
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
//案例: 输入年龄,判断年龄<18,警告
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的真实年龄");
int age = sc.nextInt();
if(age < 18){
System.out.println("警告:只能玩1个小时噢");
}
System.out.println("程序结束");
}
}
格式三:多分支结构
if(比较表达式1) { 语句体1; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } ... else { 语句体n+1; }
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
//输入你的钱 和 商品的钱 购买成功 支付失败,请充值
Scanner sc = new Scanner(System.in);
System.out.println("输入你的余额");
int money = sc.nextInt();
System.out.println("输入商品的钱的价格");
int price = sc.nextInt();
if(money>=price){ //购买成功 money>=price
System.out.println("购买成功");
}else{
System.out.println("支付失败,请充值");
}
}
}
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
//案例:输入你的分值,输出等级
Scanner sc = new Scanner(System.in);
System.out.println("输入你的分值");
int x = sc.nextInt();// -10
if( x>=90 && x<=100){
System.out.println("SSS");
}else if( x>=80){
System.out.println("SS");
}else if(x>=70){
System.out.println("S");
}else if(x>60){
System.out.println("A");
}else if(x >=0){
System.out.println("B");
}else{//其余所有情况
System.out.println("错误分数");
}
}
}
if语句的嵌套使用
2.3 switch语句的格式
switch(表达式) { case 值1: //case:如果 语句体1; break; case 值2: 语句体2; break; … default://缺省等同于else 语句体n+1; break; }
执行流程 先计算表达式的值 然后和case后面的匹配,如果有就执行对应的语句,否则执行default控制的语句
注意事项
case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的
default可以省略,但不建议
break省略会出现一个现象:case穿透。建议不要省略
default可以在任意位置。但是建议在最后
案例演示5
分别用switch语句和if语句实现:键盘录入月份,输出对应的季节
import java.util.Scanner;
public class Demo7 {
public static void main(String[] args) {
//取钱
Scanner sc = new Scanner(System.in);
System.out.println("输入密码");
int mima = sc.nextInt();
//先判断密码
if(mima == 6666){
System.out.println("进入系统");
System.out.println("输入你要取的金额");
int money = sc.nextInt();
//判断足够 1000
if(money>=1000){
System.out.println("取款成功");
}else{
System.out.println("余额不足");
}
}else{
System.out.println("密码错误,退卡");
}
}
}
import java.util.Scanner;
public class Demo8 {
public static void main(String[] args) {
//星期
Scanner sc = new Scanner(System.in);
System.out.println("输入今天星期几");
int week = sc.nextInt();
switch(week){
case 1:
System.out.println("上课");
break;
case 2:
System.out.println("休息");
break;
case 3:
System.out.println("上课");
break;
case 4:
System.out.println("休息");
break;
case 5:
System.out.println("上课");
break;
case 6:
System.out.println("休息");
break;
case 7:
System.out.println("休息");
break;
default:
System.out.println("警告:输入错误");
break;
}