第三章 选择语句

第3章 选择语句
3.1 比较运算符
大于 >
大于等于 >=
小于 <
小于等于 <=
等于 ==
不等于 !=
3.2 逻辑运算符
单与 &
双与 &&
或 |
双或 ||
非 !
异或 ^
单与 &和 双与 &&区别:
单与 &:无论左侧真还是假,都会自动判断右侧的值
双与 &&:先判断左侧,当左侧为假时,会直接报错,不再判断右侧;
同理或 和双或区别也一样,当左侧可以直接判断结果时,双或||不会再去浪费时间判断右侧,而单或总会判断两侧的值。
3.3 if语句
单if
if-else分支
多if-else嵌套
多if-else分支
示例:计算身体质量指数(身体质量指数BMI=体重/身高的平方,小于18.5为偏瘦,[18.5,25.0)为正常 [25.0,30.0)为超重 大于30.0为过胖。设计程序当用户输入体重 身高 显示他的胖瘦情况)代码如下:
import java.util.Scanner;
public class Test1 {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println(“请输入身高和体重”);
double weight=scanner.nextDouble();
double hight=scanner.nextDouble();
Double BMI=weight/hight/hight;
if (BMI>=30.0){
System.out.println(“过胖”);
}else if(BMI>=25.0){
System.out.println(“超重”);
}else if(BMI>=18.5){
System.out.println(“正常”);
}else{
System.out.println(“偏瘦”);
}

}	
}

switch语句、if else 语句
Switch可以传哪些变量:
switch 语句根据 char、 byte,short、 int 或者 String、枚举类型的 switch 表达式来进行控制决定。 在 switch 语句中, 关键字 break 是可选的, 但它通常用在每个分支的结尾, 以中止执行 switch语 句的剩余部分。 如果没有出现 break 语句, 则执行接下来的 case 语句。
switch语句基本结构:

switch(表达式){
case 取值1:
执行语句;
break;
case 取值1:
执行语句;
break;
case 取值1:
执行语句;
break;
default:
执行语句;
break;
}
if else
典型例题如下;
例题1:解一元二次方程:
import java.util.Scanner;
public class test3_1 {
public static void main (String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print(“请输入a,b,c的值”); …///获取未知数的值
double a=scanner.nextDouble();
double b=scanner.nextDouble();
double c=scanner.nextDouble();
double delt= bb-4ac; …///定义delta
if(delt>0){
double x1=((-b+Math.sqrt(delt))/2a); … //分情况讨论,公式写解
double x2=((-b-Math.sqrt(delt))/2a);
System.out.println(“The question has two real roots”+x1+x2); …黑体字为if else嵌套结构
}else if( delt ==0){
double x1=-b/2a;
System.out.println(“The question has one real root”+x1); // 输出。
}else{
System.out.println(“The question has no real root”);
}

}
}
例2: 周日为0,周一为1,…周六为6. 编写程序,使得用户输入一个代表今天的数字,再输入一个今天之后的数字后,能显示今天是周几。代码如下:
import java.util.Scanner;
public class test3_3 {
public static void main (String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println(“enter today’s day:”); …//提示用户输入今天的数字
int today =scanner.nextInt();
System.out.println(“enter the number of days elapsed since today:”); … //提示
int since =scanner.nextInt();
switch(today%7){
case 1:
System.out.print(“today is Monday”); // switch语句选择
break;
case 2:
System.out.print(“today is Tuesday”);
break;
case 3:
System.out.print(“today is Wednesday”);
break;
case 4:
System.out.print(“today is Thursday”);
break;
case 5:
System.out.print(“today is Friday”);
break;
case 6:
System.out.print(“today is Saturday”);
break;
case 0:
System.out.print(“today is Sunday”);
break;
}
switch((today+since)%7) {
case 1:
System.out.print(“the future day is Monday”);
break;
case 2:
System.out.print(“the future day is Tuesday”);
break;
case 3:
System.out.print(“the future day is Wednesday”);
break;
case 4:
System.out.print(“the future day is Thursday”);
break;
case 5:
System.out.print(“the future day is Friday”);
break;
case 6:
System.out.print(“the future day is Saturday”);
break;
case 0:
System.out.print(“the future day is Sunday”); …输出
break;
}
}
}
例3:
习题3——8
mport java.util.Scanner;
public class 练习1 {
public static void main(String[] args){
Scanner scanner =new Scanner(System.in); …//获取用户输入的年月日
System.out.println(“enter year”);
int year=scanner.nextInt();
System.out.println(“enter month”);
int month=scanner.nextInt();
System.out.println(“enter the day of the month”);
int day=scanner.nextInt();
if(month1 || month2){ …///对于题中特别要求的1、2月份要写作13、14月
month+=12;
year–;
}
int j=year/100; …//世纪数
int k=year%100; … //世纪的第几年
int h=(day+(26*(month+1))/10+k+k/4+j/4+5*j)%7;
switch(h%7){ …//用switch语句选择;0为星期六,1为星期天,以此类推。。
case 1:
System.out.println(“day of the week is sunday”);
break;
case 2:
System.out.println(“day of the week is monday”);
break;
case 3:
System.out.println(“day of the week is tuesday”);
break;
case 4:
System.out.println(“day of the week is wednesday”);
break;
case 5:
System.out.println(“day of the week is thursday”);
break;
case 6:
System.out.println(“day of the week is friday”);
break;
case 0:
System.out.println(“day of the week is saturday”);
break
}
总结:大概学会运用switch语句,if else进行简单的编程,能熟练的利用scanner函数进行输入,可以独立的编写一些不太复杂的程序代码。会继续勤加练习,争取更加熟练快速的做题、。


本文来自 zhangdepu_ 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zhangpupu320/article/details/82947002?utm_source=copy

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值