Java中定义成员变量时采用合法的前向引用。 从上往下执行
有if…else和switch-case两种分支语句
- /*
- 如果:
- 成绩为100分时,奖励一辆BMW;
- 成绩为(80,99]时,奖励一台iphone xs max;
- 当成绩为[60,80]时,奖励一个 iPad;
- 其它时,什么奖励也没有。
- 请从键盘输入岳小鹏的期末成绩,并加以判断
- */
- import java.util.Scanner;
-
- class ScoreTest{
- public static void main(String[] args){
- System.out.println("输入考试分数(0-100)");
- Scanner scanner = new Scanner(System.in);
- double score=scanner.nextDouble();
- if(score==100){
- System.out.println("奖励一辆BMW");
- }else if(score>80 && score<=99){
- System.out.println("奖励一台iphone xs max");
- }else if(score>=60 && score<=80){
- System.out.println("奖励一个 iPad");
- }else{
- System.out.println("什么奖励也没有");
- }
- }
- }
说明:
else可以没有
多分支 条件互斥的话 循序没关系
多分支条件有交集 顺序很关键 认证考虑 谨慎驾驶
多分支包含的关系 应该先写小的 在写大条件
if else 可以嵌套
只有一行执行语句省略可以大括号
- //1)对下列代码,若有输出,指出输出结果。
- int x = 4;
- int y = 1;
- if (x > 2) {
- if (y > 2)
- System.out.println(x + y);
- System.out.println("atguigu");
- } else
- System.out.println("x is " + x);
输出结果: atguigu
格式:
switch(表达式){
case 常量1:
语句1;
// break;
case 常量2:
语句2;
// break;
… …
case 常量N:
语句N;
// break;
default:
语句;
// break;
}
注意点:
① 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。
当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字或此switch-case结构
末尾结束为止。
② break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构
③ switch结构中的表达式,只能是如下的6种数据类型之一:
byte 、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)
④ case 之后只能声明常量。不能声明范围。
⑤ break关键字是可选的。
⑥ default:相当于if-else结构中的else.
default结构是可选的,而且位置是灵活的。
⑦ 凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。
⑧ 既可以使用switch-case,(同时,switch中表达式的取值情况不太多),又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。
⑨ case可以合并
例子:
- /*
- 从键盘分别输入年、月、日,判断这一天是当年的第几天
- 注:判断一年是否是闰年的标准:
- 1)可以被4整除,但不可被100整除
- 或
- 2)可以被400整除
- */import java.util.Scanner;class SwitchTest05{
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("请输入年份");
- int year = scan.nextInt();
- System.out.println("请输入月份");
- int month = scan.nextInt();
- System.out.println("请输入日期");
- int day = scan.nextInt();
- int dayIndex=0;
- switch(month){
- case 12:
- dayIndex+=30;
- case 11:
- dayIndex+=31;
- case 10:
- dayIndex+=30;
- case 9:
- dayIndex+=31;
- case 8:
- dayIndex+=31;
- case 7:
- dayIndex+=30;
- case 6:
- dayIndex+=31;
- case 5:
- dayIndex+=30;
- case 4:
- dayIndex+=31;
- case 3:
- if((year%4==0&&year%100!=0)||year%400==0){
- dayIndex+=29;
- }else{
- dayIndex+=28;
- }
- case 2:
- dayIndex+=31;
- case 1:
- dayIndex+=day;
-
- System.out.println(month+"月"+day+"日是2022年的第"+dayIndex+"天");
-
- }
- }
- }
-
- /*
- 从键盘上读入一个学生成绩,存放在变量score中,根据score的
- 值输出其对应的成绩等级:
- score>=90 等级: A
- 70<=score<90 等级: B
- 60<=score<70 等级: C
- score<60 等级: D
- */import java.util.Scanner;class SwitchTest07{
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("请输入分数");
- int score = scan.nextInt();
- // 方式一:
- /*
- if(score>=90){
- System.out.println("等级A");
- }else if(score>=70&&score<90){
- System.out.println("等级B");
- }else if(score>=60&&score<70){
- System.out.println("等级C");
- }else{
- System.out.println("等级D");
- }
- */
- // 方式二:
- int level = score/10;
- switch(level){
- case 10:
- case 9:
- System.out.println("等级A");
- break;
- case 8:
- case 7:
- System.out.println("等级B");
- break;
- case 6:
- System.out.println("等级C");
- break;
- default:
- System.out.println("等级D");
- break;
- }
-
- }
- }
if和switch语句很像,具体什么场景下,应用哪个语句呢?
1如果判断的具体数值不多,而且符合byte、short 、char、int、String、枚举等几
种类型。建议使用swtich语句。因为效率稍高。
有while、do…while、for三种循环语句。
for:
语法格式
for (①初始化部分; ②循环条件部分; ④迭代部分){
③循环体部分;
}

注意:②为Boolean类型
- /*
- 100以内的偶数
- 所有偶数的和
- 偶数的个数
- */
- class ForTest03{
- public static void main(String[] args){
- int sum = 0;
- int count = 0;
- for(int i=1;i<=100;i++){
- if(i%2==0){
- System.out.println("100以内的偶数 是"+i);
- sum+=i;
- count++;
- }
- }
- System.out.println("所有偶数的和 是"+sum);
- System.out.println("偶数的个数是"+count);
- }
- }
- /*
- 输入两个正整数m和n,求其最大公约数和最小公倍数。
- 比如:12和20的最大公约数是4,最小公倍数是60。
- 。
- */
- import java.util.Scanner;
- class ForTest02{
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("请输入一个正整数m");
- int m=scan.nextInt();
- System.out.println("请输入一个正整数n");
- int n=scan.nextInt();
- int maxDivisor=0;
- int minMultiple=0;
- int minNum = m
- int maxNum = m>=n?m:n;
- for(int i=minNum;i>0;i--){
- if(m%i==0&&n%i==0){
- System.out.println("最大公约数是"+i);
- break;
- }
- }
- // m*i==n*j
- for(int i=maxNum;i
- if(i%m==0&&i%n==0){
- System.out.println("最小公倍数是"+i);
- break;
- }
- }
- }
- }
while
格式
①初始化部分
while(②循环条件部分){
③循环体部分;
④迭代部分;
}
注意不要忘记声明④迭代部分。否则,循环将不能结束,变成死循环。
for循环和while循环可以相互转换
do...while
格式
①初始化部分;
do{
③循环体部分
④迭代部分
}while(②循环条件部分);
do-while循环至少执行一次循环体。
- /*
- 100以内的偶数
- 所有偶数的和
- 偶数的个数
- */
-
- class DoWhileTest{
- public static void main(String[] args){
- int i = 1;
- int sum=0;
- int count=0;
- do{
- if(i%2==0){
- System.out.println("100以内的偶数"+i);
- sum+=i;
- count++;
- }
- i++;
- }while(i<=100);
- System.out.println("所有偶数的和"+sum);
- System.out.println("偶数的个数"+count);
- }
- }
- /*
- 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入
- 为0时结束程序
- */
- import java.util.Scanner;
- class ForWhileTest{
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("请输入数字");
-
- int positiveCount=0,negativeCount=0;
- do{
-
- int num = scan.nextInt();
- if(num>0){
- positiveCount++;
- }else if(num<0){
- negativeCount++;
- }else{
- break;
- }
-
- }while(true);
- System.out.println("正数的个数是"+positiveCount+" 负数的个数是"+negativeCount);
- }
- }
循环嵌套:
- /*
- 100以内的所有质数
- 2:1 2
- 3:1 3
- 4:1 2 4
- */
- class PrimeNum2{
- public static void main(String[] args){
- //获取当前时间距离1970-01-01 00:00:00 的毫秒数
- long start = System.currentTimeMillis();
- boolean isFlag = true;
- int primeCount = 0;
- for(int i=2;i<=10000;i++){
- for(int j=2;j<=Math.sqrt(i);j++){
- if(i % j == 0){
- isFlag = false;
- break;// 优化一:对本身非质数的数有效的
- }
- }
- if(isFlag==true){
- primeCount++;
- }
- isFlag = true;
- }
- //获取当前时间距离1970-01-01 00:00:00 的毫秒数
- long end = System.currentTimeMillis();
- System.out.print("质数个数是"+primeCount);
- System.out.println("所花费的时间为:" + (end - start));
- }
- }
break continue
- /*
- break和continue关键字的使用
- 使用范围 循环中使用的作用(不同点) 相同点
- break: switch-case
- 循环结构中 结束当前循环 关键字后面不能声明执行语句
- continue: 循环结构中 结束当次循环 关键字后面不能声明执行语句
- */
- class BreakContinueTest {
- public static void main(String[] args) {
-
- for(int i = 1;i <= 10;i++){
-
- if(i % 4 == 0){
- break;//123
- //continue;//123567910
- //System.out.println("今晚迪丽热巴要约我!!!");
- }
- System.out.print(i);
- }
-
- System.out.println("\n");
- //******************************
-
- label:for(int i = 1;i <= 4;i++){
-
- for(int j = 1;j <= 10;j++){
-
- if(j % 4 == 0){
- //break;//默认跳出包裹此关键字最近的一层循环。
- //continue;
-
- //break label;//结束指定标识的一层循环结构
- continue label;//结束指定标识的一层循环结构当次循环
- }
-
- System.out.print(j);
- }
-
- System.out.println();
- }
- }
- }
-
相关阅读:
Spring Cloud Netflix之OpenFeign
docker--知识点提炼
大数据安全的重要性解读
【java】3-获取线程引用与线程的属性
基于Redis实现消息队列的实践
授权调用: 介绍 Transformers 智能体 2.0
docker 安装 redis 6.0.8 cluster 实战 (3主3从) 动态缩容
Flutter(学前必看)基础
Linux 日志系统
企业邮箱选择指南:最适合跨境贸易的解决方案推荐
-
原文地址:https://blog.csdn.net/hongc93/article/details/127616867