练习1
根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n <= 18) { System.out.println("少年"); } else if(n <= 28) { System.out.println("青年"); } else if(n <= 55) { System.out.println("中年"); } else { System.out.println("老年"); } sc.close(); } }
练习2
判定一个数字是否是素数
import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i = 2; boolean a = true; while(i <= n/2) { if(n%i == 0) { a = false; break; } } if(a) { System.out.println(n+"是素数"); } else { System.out.println(n+"不是素数"); } sc.close(); } }
练习3
打印 1 - 100 之间所有的素数
public class PrintPrime { public static void main(String[] args) { int n = 1; for(n = 1;n <= 100;n++) { int i = 2; boolean a = true; while(i <= n/2) { if(n%i == 0) { a = false; break; } i++; } if(a) { System.out.print(n+" "); } } } }
练习4
输出 1000 - 2000 之间所有的闰年
public class LeapYear { public static void main(String[] args) { int a = 1000; for(a = 1000;a < 2000;a++) { if(a%100 == 0) { if(a%400 == 0) { System.out.println(a+"是闰年"); } } else if(a%4 == 0) { System.out.println(a+"是闰年"); } } } }
练习5
输出乘法口诀表
public class MultiplicationTable { public static void main(String[] args) { int i = 0; int j = 0; for(i = 1;i <= 9;i++) { for(j = 1;j <= i;j++) { System.out.printf("%d*%d=%d ",j,i,i*j); } System.out.println(); } } }
练习6
求两个正整数的最大公约数
import java.util.Scanner; public class CommonDivisor { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); boolean con = true; int c = 1; for( c = a<b?a:b;c>1;c--) { if(a%c == 0&&b%c == 0) { System.out.println(c+"为最大公约数"); con = false; break; } } if(con) { System.out.println("没有公约数"); } } }
练习7
求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,
如: 153=1^3+5^3+3^3 ,则153是一个“水仙花数”。)
public class NarcissisticNumber { public static void main(String[] args) { int i = 1; int x = 0; double w = 1.0; for(i = 0;i<1000;i++) { x = 0; double p = Math.pow(10.0,w); while (i/Math.pow(10.0,w) > 1) { p = i/Math.pow(10.0,w); w++; } int j = i; while (j>0) { x += Math.pow(j%10,w); j /= 10; } if (x == i) { System.out.println(i); } } } }
练习8
写一个函数返回参数二进制中 1 的个数,比如: 15 0000 1111 4 个 1
import java.util.Scanner; public class BinarySystem { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int count = 0; while ( n != 0) { if ((n&1) == 1) { count++; } n >>= 1; } System.out.println(count); sc.close(); } }
练习9
获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
import java.util.Scanner; public class BinarySequence { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int j = n; int o = n; int i = 0; while(i < 16) { System.out.print(j&1); j >>= 2; i++; } System.out.println(); o >>= 1; i = 0; while(i < 16) { System.out.print(o&1); o >>= 2; i++; } } }
总结
关于《程序逻辑控制练习代码》就讲解到这儿,欢迎各位留言交流以及批评指正,如果文章对您有帮助或者觉得作者写的还不错可以点一下关注,点赞,收藏支持一下。