Java?SE循環(huán)一些基本練習(xí)題總結(jié)
判定一個(gè)數(shù)字是否是素?cái)?shù)
public class Test { public static int is_sushu(int n) { if(n == 1) { return 0; } int i ; for (i = 2; i <= Math.sqrt(n); i++) { if(n % i == 0 ) { break; } } if (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 + "是素?cái)?shù)"); } else { System.out.println(n + "不是素?cái)?shù)"); } } }
輸出 1000 - 2000 之間所有的閏年
public class Test { public static void main(String[] args) { int year ; for ( year = 1000; year <= 2000 ; year++) { if(year % 4 ==0 && year % 100 !=0 || year % 400 == 0 ) { System.out.println(year + "是閏年"); } } } }
輸出乘法口訣表
public class Test { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i ; j++) { System.out.print(i + "*" + j + "=" + i*j + " "); } System.out.println(); } } }
求兩個(gè)正整數(shù)的最大公約數(shù)
public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); if(b > a) { int tmp = b; b = a; a = b; } int c =a % b; while(c != 0) { a = b; b = c; c = a % b; } System.out.println(b); } }
求出0~999之間的所有“水仙花數(shù)”并輸出。
(“水仙花數(shù)”是指一個(gè)三位數(shù),其各位數(shù)字的立方和確好等于該數(shù)本身,如: 153=1^3+5^3+3^3 ,則153是一個(gè)“水仙花數(shù)”。)
public class Test { public static void main(String[] args) { int i; for (i = 100; i < 1000 ; i++) { int k = i; int sum = 0; while(k != 0) { sum+=Math.pow(k%10,3); k /= 10; } if (sum == i) { System.out.println(i); } } } }
寫一個(gè)函數(shù)返回參數(shù)二進(jìn)制中 1 的個(gè)數(shù)
比如: 15 0000 1111 4 個(gè) 1
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int count = 0; /*for (int i = 0; i < 32; i++) { if ((n >> i & 1) == 1) { count++; } }*/ while (n != 0) { count++; n = n & (n-1); } System.out.println(count); } }
獲取一個(gè)數(shù)二進(jìn)制序列中所有的偶數(shù)位和奇數(shù)位,分別輸出二進(jìn)制序列。
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("偶數(shù)位:"); 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("奇數(shù)位:"); for ( i = 30; i >= 0; i-=2) { if((n >> i & 1) == 1) { System.out.print(1 + " "); } else { System.out.print(0 + " "); } } } }
猜數(shù)字游戲
import java.util.Random; import java.util.Scanner; public class Test { public static void main(String[] args) { //獲取隨機(jī)數(shù) 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("輸入你的數(shù)字"); int ret = scanner.nextInt(); if(ret > n) { System.out.println("猜大了"); } else if(ret < n) { System.out.println("猜小了"); } else { System.out.println("恭喜你猜對了"); break; } count--; if(count == 0) { System.out.println("你沒有機(jī)會(huì)了"); break; } System.out.println("你還有" + count + "機(jī)會(huì)"); } } }
總結(jié)
到此這篇關(guān)于Java SE循環(huán)一些基本練習(xí)題的文章就介紹到這了,更多相關(guān)Java SE循環(huán)練習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了java圖片滑動(dòng)登錄驗(yàn)證的相關(guān)原理、實(shí)現(xiàn)方法與操作技巧,需要的朋友可以參考下2019-09-09Java把Map轉(zhuǎn)為對象的實(shí)現(xiàn)代碼
在項(xiàng)目開發(fā)中,經(jīng)常碰到map轉(zhuǎn)實(shí)體對象或者對象轉(zhuǎn)map的場景,工作中,很多時(shí)候我們可能比較喜歡使用第三方j(luò)ar包的API對他們進(jìn)行轉(zhuǎn)化,但這里,我想通過反射的方式對他們做轉(zhuǎn)化,感興趣的同學(xué)跟著小編來看看吧2023-08-08Spring?component-scan?XML配置與@ComponentScan注解配置
這篇文章主要介紹了Spring?component-scan?XML配置與@ComponentScan注解配置,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09ZooKeeper入門教程二在單機(jī)和集群環(huán)境下的安裝搭建及使用
本文是ZooKeeper入門系列教程,涵蓋ZooKeeper的安裝使及單機(jī)集群環(huán)境搭建,通過實(shí)例和大量圖表,結(jié)合實(shí)戰(zhàn),幫助學(xué)習(xí)者理解和運(yùn)用,有需要的朋友可以借鑒參考下2022-01-01