【Java全栈学习笔记-U1-day03】if语句
文章目录
day03-if语句
回顾
八大基本类型:byte、short、int、long、float、double、boolean、char
字符串型:String
示例:理解输入
输入一个长方形的长和宽,求周长。
思路:输入长和宽→计算周长输出结果
import java.util.Scanner;
public class Demo1 {
//强化输入
public static void main(String[] args) {
//1.导入Scanner类
//import java.util.Scanner;
//2.创建对象
Scanner input = new Scanner(System.in);
//3.接收输入
//input.next(); //接收输入字符串
//input.nextInt(); //接收输入整数
//input.nextDouble(); //接收输入小数
System.out.println("请输入长:");
double length = input.nextDouble();
System.out.println("请输入宽:");
double width = input.nextDouble();
//计算
double result = (length + width) * 2;
//输出
System.out.println("周长是 :" + result);
}
}
课程目标
1.理解程序的控制结构
2.掌握if语句
3.会逻辑运算符
4.掌握if嵌套语句
5.掌握多重if语句
一、程序的控制结构
1.1 控制结构分类
顺序结构、选择结构(if语句、switch语句)、循环结构(while、do…while、for)
二、选择结构
2.1 什么是选择结构
选择结构是根据条件判断之后再做出处理
2.2 if选择结构的语法
if(条件){
条件成立时执行的代码块
}else{
条件不成立时执行的代码块
}
注意:
a.条件一般为关系表达式或者逻辑表达式、返回结果为boolean类型的表达式都可以
b.else块可有可无
c.当条件成立时执行的代码只有一行时,可省略大括号不写
示例:
public static void main(String[] args) {
//理解if语句
//输入语文考试成绩,显示是否通过考试(60标准)
Scanner input = new Scanner(System.in);
System.out.println("请输入语文成绩:");
int Chinese = input.nextInt(); //50
//判断
if(Chinese < 60) {
System.out.println("不及格");
}
else {
System.out.println("通过考试");
System.out.println("奖电脑一台");
}
}
3.嵌套if语句
所谓嵌套if语句就是在一个if语句中再使有if语句
语法:
if(条件){
if(条件){….}else{…}
}else{
if(条件){….}else{…}
}
示例:
public static void main(String[] args) {
//嵌套if语句
//输入语文数学成绩判断是否通过考试,没有通过给出相应的提示
Scanner input = new Scanner(System.in);
System.out.println("请输入语文成绩:");
int Chinese = input.nextInt();
System.out.println("请输入数学成绩:");
int math = input.nextInt();
if (Chinese >= 60){
//语文通过,再根据数学判断
if (math >= 60){
System.out.println("两科都通过了考试。");
}else {
System.out.println("数学不及格,没通过。");
}
}else {
//语文没有通过
if(math >= 60){
System.out.println("语文不及格,没通过。");
}else {
System.out.println("两科均不及格,没有通过考试。");
}
}
}
4.多重if语句
if(条件){
...
}else if(条件){
...
}else if(条件){
...
}else{
...
}
示例:
public static void main(String[] args) {
//理解多重if
//输入语文考试成绩,显示评级
//优(80以上) 中(60-80) 差(60以下)
Scanner input = new Scanner(System.in);
System.out.println("请输入语文成绩:");
int chinese = input.nextInt();
if(chinese >= 80){
System.out.println("优");
}else if(chinese >= 60){
System.out.println("中");
}else {
System.out.println("差");
}
}
三、逻辑运算符
1.逻辑运算符的作用:
连接多个关系表达式
示例:
关系表达式 && 关系表达式
2.运算符
运算符 | 表达式 | 说明 |
---|---|---|
&& | 条件1 && 条件2 | 仅两个条件同时为真,结果为真 |
|| | 条件1 || 条件2 | 只要两个条件有一个为真,结果为真 |
! | ! 条件 | 条件为真时,结果为假 条件为假时,结果为真 |
|| 或者:左右两边有一边为真结果为真
&& 且:左右两边同时为真结果为真
! 非:非真非假
示例:
public static void main(String[] args) {
//理解逻辑运算符
//输入语文和数学成绩判断是否通过考试
//都大于60,通过;只要有一门小于60就不通过
Scanner input = new Scanner(System.in);
System.out.println("请输入语文成绩:");
int chinese = input.nextInt();//接收输入
System.out.println("请输入数学成绩:");
int math = input.nextInt();
//逻辑运算符
if (chinese >= 60 && math >= 60){
System.out.println("通过考试");
}else {
System.out.println("活着好累啊,这都通不过");
}
}
四、理解流程图
1.流程图的元素
椭圆 表示程序的开始和结束
平形四边形 表示程序的输入
矩形 表示执行的代码块
菱形 表示判断
箭头 表示走向
2. 流程图示例(实现幸运抽奖)
3.幸运抽奖
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//3.生成随机数 Math.random产生0-1的随机小数
int temp = (int)(Math.random() * 10);
System.out.println("随机数:" + temp);
//1.输入会员卡号
System.out.println("请输入四位的随机卡号:");
int cardNo = input.nextInt();
//2.获得随机卡号的百位
int hundredsPlace = cardNo / 100 % 10;
//4.判断 相等两个等于号
if(hundredsPlace == temp){
System.out.println("恭喜中奖");
}else {
System.out.println("不好意思您没有中奖");
}
}
作业
1.综合练习模拟实现银行取款机实现存款,取款
思路: 定义money变量存总额 int money=5000;
效果一如下:
取款机的功能项为:1.存款 2.取款
请你选择功能:1
请输入存款金额:300
余额为:5300
请你选择功能:2
请输入取款金额:300 考虑超额
余额为:4700
肯定会用嵌套if
import java.util.Scanner;
/**
* @Author: 开心就好9905
* @Date: 2024/2/15 17:50
* @Java version: 1.8.0_361
* @Project day03
* @Package homework
* @Classname ATM
* @Created by Administrator
* @Description:模拟实现银行取款机实现存款,取款
*/
public class ATM {
public static void main(String[] args) {
int total = 5000;//总额
int func; //功能
int deposit; //存款数额
int withdraw; //取款金额
int balance; //余额
System.out.println("取款机的功能项为:1.存款\t2.取款");
System.out.println("请你选择功能:");
Scanner input = new Scanner(System.in);
func =input.nextInt();
if (func == 1){
System.out.print("请输入存款金额:");
deposit = input.nextInt();
balance = total + deposit;
System.out.println("余额为:" + balance);
}else if (func == 2){
System.out.print("请输入取款金额:");
withdraw = input.nextInt();
if (withdraw > total){
System.out.println("您的余额不足,无法取出该数量的纸币。");
}else {
balance = total - withdraw;
System.out.println("余额为:" + balance);
}
}else {
System.out.println("没有该功能,请重新输入。");
}
}
}
2.实现计算总页数的功能: 要求是:每页3行信息
效果如下:
请输入总行数:10
对应的总页数为:4
实现思路: 总页数 除 每页行数
import java.util.Scanner;
/**
* @Author: 开心就好9905
* @Date: 2023/2/12 17:00
* @Description:2.实现计算总页数的功能: 要求是:每页3行信息
*/
public class Pages {
public static void main(String[] args) {
int line; //总行数
int num = 3; //每页行数
int leftover;
int result;
System.out.print("请输入总行数:");
Scanner input = new Scanner(System.in);
line = input.nextInt();
leftover = line % num;
if (leftover == 0){
result = line / num;
System.out.println("对应总页数为:" + result);
}else if (leftover != 0){
result = line / num + 1;
System.out.println("对应总页数为:" + result);
}
}
}
3.模拟实现商超打折程序
需求是:客户购物
金额达到1000打七折。
金额达到800打八折。
金额达到500打九折。
500以下打九八折
效果如下:
请输入购物金额:600
实际支付为:540
多重if
import java.util.Scanner;
/**
* @Author: 开心就好9905
* @Date: 2023/2/12 18:37
* @Description:3.模拟实现商超打折程序
*/
public class Discounts {
public static void main(String[] args) {
int price; //购物金额
double pay; //实际支付金额
System.out.print("请输入购物金额:");
Scanner input = new Scanner(System.in);
price = input.nextInt();
if (price < 0){
System.out.println("金额不正确,请重新输入!");
}else if (price >= 0 && price < 500){
pay = price * 0.98;
System.out.println("实际支付为:" + pay);
}else if (price >= 500 && price < 800){
pay = price * 0.9;
System.out.println("实际支付为:" + pay);
}else if (price >= 800 && price < 1000){
pay = price * 0.8;
System.out.println("实际支付为:" + pay);
}else if (price >= 1000){
pay = price * 0.7;
System.out.println("实际支付为:" + pay);
}
}
}
4.模拟实现小型计算器:实现加减乘除
效果如下:
请输入第一个数:10
请输入符号:+ “接收输入字符串”
请输入第二个数:10
结果为:20
提示:通过符号判断实现相应功能,多重if语句实现
判断字符串相等
If(变量. equals(要比较的符号)){
}
import java.util.Scanner;
/**
* @Author: 开心就好9905
* @Date: 2023/2/12 18:49
* @Description:4.模拟实现小型计算器:实现加减乘除
*/
public class MiniCalculator {
public static void main(String[] args) {
double a; //第一个数
double b; //第二个数
String symbol; //运算符
double result; //结果
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个数:");
a = input.nextDouble();
System.out.print("请输入符号:");
symbol = input.next();
System.out.print("请输入第二个数:");
b = input.nextDouble();
if (symbol.equals("+")) {
result = a + b;
System.out.println("结果为:" + result);
} else if (symbol.equals("-")) {
result = a - b;
System.out.println("结果为:" + result);
} else if (symbol.equals("*")) {
result = a * b;
System.out.println("结果为:" + result);
} else if (symbol.equals("/")) {
result = a / b;
System.out.println("结果为:" + result);
}
}
}
选做
实现电脑自动根据用户上机时间进行问好?
要求:
6-8 提示早上好
12-14提示中午好
8-12 14-17提示:安心上网,无人打拢
20-22提示 晚上好 其它时间提示:睡觉哦!
提示如下:
Date date=new Date(System.currentTimeMillis()); //获取系统日期
System.out.println(date.getHours());//获取当前的小时
import java.util.Date;
/**
* @Author: 开心就好9905
* @Date: 2023/2/12 19:09
* @Description:5.实现电脑自动根据用户上机时间进行问好
*/
public class Greeting {
public static void main(String[] args) {
int hours; //当前小时
Date date = new Date(System.currentTimeMillis());
System.out.println(date);
//System.out.println(date.getHours());
hours = date.getHours();
if (hours >= 6 && hours < 8){
System.out.println("早上好~");
}else if (hours >= 8 && hours <12){
System.out.println("安心上网,无人打扰~");
}else if (hours >= 12 && hours <14){
System.out.println("中午好~");
}else if (hours >= 14 && hours <17){
System.out.println("安心上网,无人打扰~");
}else if (hours >= 17 && hours <20){
System.out.println("该吃晚饭了~");
}else if (hours >= 20 && hours <22){
System.out.println("晚上好~");
}else if (hours >= 22 && hours <= 24){
System.out.println("睡觉哦!");
}else if (hours >= 0 && hours <6){
System.out.println("睡觉哦!");
}
}
}
6.请用户输入三个数,显示三个的由大到小的顺序
请输入第一个数:2
请输入第二个数:7
请输入第三个数:4
显示的顺序为:7 4 2
要求使用判断语句实现
import java.util.Scanner;
/**
* @Author: 开心就好9905
* @Date: 2023/2/12 19:18
* @Description:6.请用户输入三个数,显示三个的由大到小的顺序
*/
public class Orders {
public static void main(String[] args) {
int first; //第一个数
int second; //第二个数
int third; //第三个数
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个数:");
first = input.nextInt();
System.out.print("请输入第二个数:");
second = input.nextInt();
System.out.print("请输入第三个数:");
third = input.nextInt();
if (first > second){
if (third < second){
System.out.println("显示的顺序为:" + first + " " + second + " " + third);
}else if (third > second){
if (third > first){
System.out.println("显示的顺序为:" + third + " " + first + " " + second);
}else if (third < first){
System.out.println("显示的顺序为:" + first + " " + third + " " + second);
}
}
}else if (first < second){
if (third < first){
System.out.println("显示的顺序为:" + second + " " + first + " " +third);
}else if (third > first){
if (third > second){
System.out.println("显示的顺序为:" + third + " " + second + " " + first);
}else if (third < second){
System.out.println("显示的顺序为:" + second + " " + third + " " + first);
}
}
}
}
}