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)文章
SpringCloud服務(wù)注冊中心數(shù)據(jù)一致性問題
這篇文章主要介紹了SpringCloud服務(wù)注冊中心數(shù)據(jù)一致性問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
解決@Scheduled定時(shí)器使用@Thransactional事物問題
這篇文章主要介紹了解決@Scheduled定時(shí)器使用@Thransactional事物問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
RocketMQ之Consumer整體介紹啟動(dòng)源碼分析
這篇文章主要為大家介紹了RocketMQ源碼分析之Consumer整體介紹啟動(dòng)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
MyBatis實(shí)現(xiàn)簡單的數(shù)據(jù)表分月存儲
本文主要介紹了MyBatis實(shí)現(xiàn)簡單的數(shù)據(jù)表分月存儲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

