程序逻辑结构

程序逻辑结构程序逻辑控制 3 种结构的认识 程序逻辑结构

大家好,欢迎来到IT知识分享网。

1、顺序结构:根据代码书写顺序一行行执行

2、选择结构:

(1)if(布尔表达式){

                        语句1;

                        }else{

                        语句2;

                        }

(2)switch(表达式){

case

常量值
1
:{
语句
1
;
[
break
;]
}
case

常量值
2
:{
语句
2
;
[
break
;]
}
default
:{
内容都不满足时执行语句
;
[
break
;]
}
}

3、循环结构:

(1)for(表达式1;布尔表达式,循环条件;表达式3){

         语句;

           }

(2)while(循环条件){

          语句1;

          }else{

          语句2;

          }

(3)do{

          循环语句;

           }while(循环条件);//至少要循环一次

二、选择结构 

1、判断一个数是奇数还是偶数

import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); if(a%2==0){ System.out.println("偶数"); }else { System.out.println("奇数"); } } }

2、判断一个数是正数、负数还是0

 public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a= scanner.nextInt(); if(a<0){ System.out.println("负数"); } else if (a>0) { System.out.println("正数"); }else { System.out.println("0"); } }

3、用switch表示周一到周五,并且任意选择。

 public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int day= scanner.nextInt(); switch (day){ 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; default: System.out.println("输入错误"); break; } }

注意:

1、else只与离它最近的if匹配,写代码的时候注意编码风格。

2、最好每个选项case里面都带着一个break语句,这样程序不会继续向下运行,会从当前代码里面跳出来。

3、case后面的变量不能一样,switch不能表达复杂条件,虽然支持嵌套,但一般不建议使用。

4、在Java中不能做switch的参数数据类型:long   float  double   boolean

5、可以做switch的参数类型:

基本类型:byte、char、short、int,注意不能是long类型

引用类型:String常量串、枚举类型

三、循环语句

1、计算1-100的和

public static void main(String[] args) { int i=1; int sum=0; while (i<=100){ sum+=i; i++; } System.out.println(sum); }

2、计算1-100的奇数和与偶数和

 public static void main(String[] args) { int i=1; int sum1=0; int sum2=0; while (i<=100){ if (i%2==0){ sum1+=i; }else{ sum2+=i; } i++; } System.out.println(sum1); System.out.println(sum2); }

3、计算n的阶乘和

public static void main(String[] args) { int a=1; int sum=0; while(a<=5){//外层求阶乘的和 int ret=1;//每次求阶乘都要从1开始 int i=1; while(i<=a){//里层求每个阶乘 ret*=i; i++; } sum+=ret; a++; } System.out.println(sum); }

注意:阶乘每次都得从1开始,所以阶乘不能放在循环外面

4、找出1-100中既是3的倍数也是5的倍数的数字

public static void main(String[] args) { for (int i = 1; i <=100 ; i++) { if (i%3==0&&i%5==0){ //(i%15==0) System.out.println(i); } continue; } }

注意:continue是结束本次循环,而break是跳出循环

5、猜数字游戏,系统自动生成一个随机整数(1-100), 然后由用户输入一个猜测的数字. 如果输入的数字比该随机数小, 提示低 了”, 如果输入的数字比该随机数大, 提示高了” , 如果输入的数字和随机数相等, 则提示猜对了” .

Random  random=new  Random(  );

int n = random.nextInt (bound:100) //生成0-100的随机数,不包含100

public static void main(String[] args) { Random random=new Random(); int n= random.nextInt(100); System.out.println("随机数"+n); Scanner scanner=new Scanner(System.in); while (true){ System.out.println("输入你猜测的数字"); int a= scanner.nextInt(); if(a<n){ System.out.println("猜小了"); } else if (a>n) { System.out.println("猜大了"); }else { System.out.println("猜对了"); break;//直接跳出循环 } }

 6、根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int age = scanner.nextInt(); if (age <=18) { System.out.println("少年"); } else if (19 <=age && age <=28) { System.out.println("青年"); } else if (29 <=age && age <=55) { System.out.println("中年"); } else if (age > 56) { System.out.println("老年"); } }

7、判定一个数字是否是素数

素数:整除1和它本身

法1:假如判断7是否是素数,需要判断7是否能被2-6数字整除

法2:假如判断16是否是素数,16=1*16=2*8=4*4   ,肯定其中一个乘积会小于等于n/2

法3:或者也可以判断<=n开平方,效率更加高

public static void main1(String[] args) { int a=7;int i = 2; for (; i <=6; i++) { //法1 if(a%i==0) { System.out.println("不是素数"); break; } } if (i==a){ //不能把它放在循环里面,只有循环每一位判断结束后,才可以说是素数 System.out.println("是素数"); } } public static void main2(String[] args){ int a=7; int i=2; for(; i <=a/2; i++) { //法2 if(a%i==0) { System.out.println("不是素数"); break; } } if (i>a/2){ //不能把它放在循环里面,只有循环每一位判断结束后,才可以说是素数 System.out.println("是素数"); } } public static void main3(String[] args){ int a=7; int i=2; for(; i <=√a; i++) { //法3 if(a%i==0) { System.out.println("不是素数"); break; } } if (i>√a){ //不能把它放在循环里面,只有循环每一位判断结束后,才可以说是素数 System.out.println("是素数"); } } 

8、 输出 1000 – 2000 之间所有的闰年

1、被400整除得0

2、被100整除不等于0并且被4整除等于0

 public static void main(String[] args) { for (int j = 1000; j <=2000; j++) { if ((j%100!=0&&j%4==0)||j%400==0){ System.out.println("是闰年"+j); } } }

 9、输出乘法口诀表

public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n= scanner.nextInt(); for (int i = 1; i <=n; i++) { //控制行 for (int j = 1; j <=i; j++) { //控制列 System.out.print(i+"*"+j+"="+i*j+" "); } System.out.println(); } }

10、求两个正整数的最大公约数

辗转相除法,两个正整数相除,直到最后取模为0,就得出是最大公约数

 public static void main(String[] args) { int a=24; int b=18; int c=a%b; while (c!=0){ a=b; b=c; c=a%b; } System.out.println(b); }

 11、求出0999之间的所有水仙花数并输出。(“水仙花数是指一个三位数,其各位数字的立方和确好等于该数 本身,如: 1531^35^33^3 ,则153是一个水仙花数)

水仙花数:

1、判断是几位数

2、这几位数都分别由哪些数字组成

3、每位数是几,位数次幂算出来求和  Math.pow( 这个数值,次方),pow函数返回值类型为double类型。

public static void main(String[] args) { for (int i = 1; i < 99999; i++) { //产生这些数字 //判断有几位数并且记录下来 int count=0; int sum=0; int tmp=i; while (tmp!=0){ //知道会产生几位数 tmp/=10; count++; } //得到每一位数 tmp = i; while (tmp!=0){ sum+=Math.pow(tmp%10,count); //没位数的次方和 tmp/=10; } if (sum==i){ System.out.println(i); } } }

 12、写一个函数返回参数二进制中 1 的个数 ,比如:15:0000 1111     4 1

法1:将二进制数右移&1,不管这个数是不是为0都要移动31次,得到1的个数

法2:先判断这个数字是不是为0,如果为0直接不用进行移动,更加完善

法3:层次优化   7&6=6 ,6&5=4 ,4&3=0 ,总共&了3次,所以有3个1的个数

 

public static void main15(String[] args) { int i=7; int count=0; for (int j = 0; j < 32; j++) { if(((i>>j)&1)!=0){ count++; } } System.out.println(count); } public static void main16(String[] args) { int i=7; int count=0; while (i!=0){ if ((i&1)!=0){ //先判断数字有无等于0 count++; } i=i>>>1; //无符号右移 } } public static void main(String[] args) { int i=7; int count=0; while (i!=0){ i=i&(i-1); count++; } System.out.println(count); } } 

13、获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。

主要是奇偶数条件不同

奇数位,从i=31,i-=2;每一位&1,打印出来

偶数位,从i=30,i-=2;每一位&1,打印出来

public static void main(String[] args) { int i=7; for (int j = 31; j >=1; j-=2) { //数值位有31位 System.out.print(((i>>j)&1) +" "); } System.out.println(); for (int j = 30; j >=1 ; j-=2) { System.out.print(((i>>j)&1) +" "); } System.out.println(); }

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/118962.html

(0)
上一篇 2025-11-09 08:15
下一篇 2025-11-09 08:26

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信