行业知识
【Java SE】循环一些基本练习
2026-12-05 03:33:34
来源:互联网转载
判定一个数字是否是素数
public class Test { public static int is_sushu(int n) { if(n == 1) { return 0; } int i ; for (i = 2; i n) { return 1; } return 0; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int ret = is_sushu(n); if(ret == 1 ) { System.out.println(n + "是素数"); } else { System.out.println(n + "不是素数"); } }} 输出 1000 - 2000 之间所有的闰年
public class Test { public static void main(String[] args) { int year ; for ( year = 1000; year i & 1) == 1) { count++; } }*/ while (n != 0) { count++; n = n & (n-1); } System.out.println(count); }} 获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。
import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int i; // 00000000 00000000 00000000 00001010 // 00000000 00000000 00000000 00000001 // 00000000 00000011 // 00000000 00000000 System.out.print("偶数位:"); for ( i = 31; i > 0; i-=2) { if((n >> i & 1) == 1) { System.out.print(1 + " "); } else { System.out.print(0 + " "); } } System.out.println(""); System.out.print("奇数位:"); for ( i = 30; i >= 0; i-=2) { if((n >> i & 1) == 1) { System.out.print(1 + " "); } else { System.out.print(0 + " "); } } }}
猜数字游戏
impindex.php/tags-1038.html" >ort java.util.Random;import java.util.Scanner;public class Test { public static void main(String[] args) { //获取随机数 Random random = new Random(); Scanner scanner = new Scanner(System.in); int n = random.nextInt(100);// [0,100) int count = 3; while (count != 0) { System.out.println("输入你的数字"); int ret = scanner.nextInt(); if(ret > n) { System.out.println("猜大了"); } else if(ret
上一篇:苹果电脑怎么链接服务器?