Java小白看了都會的12道程式設計練習題!超詳細
前言
可能有很多剛入門的小白不知道自己如何能快速提升程式設計技巧與熟練度,其實大佬進階之路只有一個:那就是瘋狂碼程式碼!實踐出真知!當然大佬總結的學習筆記也是少不了的。
2021年春招,Java後端最全面試攻略,吃透25個技術棧
練習題1
接收使用者輸入的3個整數,並將它們的最大值作為結果輸出
package cn。cxy。exec;
import java。util。Scanner;
public class GetMaxNumber {
public static void main(String[] args) {
//1。提示並接收使用者輸入的三個整數,並交由變數a b c來儲存
System。out。println(“請輸入第一個整數:”);
int a = new Scanner(System。in)。nextInt();
System。out。println(“請輸入第二個整數:”);
int b = new Scanner(System。in)。nextInt();
System。out。println(“請輸入第三個整數:”);
int c = new Scanner(System。in)。nextInt();
//2。對接收到的三個值進行比較
/**三元運算子 1 ? 2 : 3
* 如果1號位置的表示式結果為true,結果就取2號位置的值,反之,就取3號位置的值*/
//2。1定義變數max來儲存a和b中的最大值
int max = a>b ? a : b;
//2。2比較max與c誰最大,並將最大值交由max來儲存
max = max>c ? max : c;
/**解決方案二:*/
//int max = a>b?(a>c?a:c):(b>c?b:c);
//3。輸出結果
System。out。println(“三個數的最大值是:”+max);
}
}
練習題2
BMI 指數測試 BMI = 體重 (kg) / 身高 (m)
接收使用者輸入的身高和體重,將判斷結果輸出
過輕:低於18。5
正常:18。5 ~ 22。9
偏胖:23 ~ 24。9
肥胖:25 ~ 29。9
重度肥胖:高於30
極度肥胖:高於40
package cn。cxy。exec;
import java。util。Scanner;
public class TestBMI {
public static void main(String[] args) {
//1。提示並接收使用者輸入的身高與體重資訊
System。out。print(“請輸入您的身高(單位為m):”);
double height = new Scanner(System。in)。nextDouble();
System。out。print(“請輸入您的體重(單位為kg):”);
double weight = new Scanner(System。in)。nextDouble();
//2。呼叫getBMI()方法,根據身高和體重資訊,輸出結果
getBMI(height, weight);
}
public static void getBMI(double h, double w) {
//求出BMI指數
double bmi = w / (h * h);
//定義一個變數r來儲存最終結果
String r = “”;
//根據 bmi 指數範圍,來給r重新賦值
if (bmi < 18。5) {
r = “過輕”;
} else if (bmi <= 22。9) {
r = “正常”;
} else if (bmi <= 24。9) {
r = “偏胖”;
} else if (bmi <= 29。9) {
r = “肥胖”;
} else if (bmi <= 40) {
r = “重度肥胖”;
} else {
r = “極度肥胖”;
}
//列印最終結果
System。out。println(“您的BMI指數:” + bmi);
System。out。println(“您的體重屬於:” + r);
}
}
練習題3
手機選號:根據使用者輸入的手機號來確認使用者實際支付的價格
如果尾數為8,需支付辦卡費50元
如果尾數為4,需支付辦卡費用0元
如果是其他尾號,需支付辦卡費用20元
package cn。cxy。exec;
import java。util。Scanner;
public class PayCard {
public static void main(String[] args) {
//1。提示並接收使用者輸入的手機號
System。out。println(“請輸入您預選的手機號:”);
String tel = new Scanner(System。in)。nextLine();//注意String型別資料一般用nextLine()
//2。呼叫getPrice()方法,來計算價格,注意需要把手機號tel作為引數傳入方法中
getPrice(tel);
}
//3。建立getPrice()
public static void getPrice(String tel) {
//3。1 手機號tel的長度,必須是11位的正確手機號——提前校驗,提高程式健壯性
if(tel。length() != 11) {
System。out。println(“號碼格式錯誤”);
return;//結束方法,返回到呼叫位置再繼續執行
}
/*比如我們拿到的手機號是tel=“2313123123”,想要拿到尾數,也就是最後一個字元
* 需要用到 char c = tel。charAt(10)
* 注意:由於下標也是從0開始,所以取第11個字元,它的下標位置是10
*/
//3。2獲取手機號的尾數
char c = tel。charAt(10);
//3。2定義用來儲存最終結果的變數price,初始值為0
int price = 0;
//3。3根據c字元的值,來為price重新賦值
switch(c) {
case ‘8’: price=50; break;//尾數為8支付50
case ‘4’: price=0; break;//尾數為4免費
default: price=20;//其他情況需支付20
}
//3。4顯示結果
System。out。println(“您實際應支付的價格為:”+price);
}
}
練習題4
分別透過for迴圈/While迴圈/do-While迴圈寫一個死迴圈
package cn。cxy。exec;
public class DeadCycle {
public static void main(String[] args) {
//for迴圈的死迴圈
// for (int i = 1; ; i++){
// System。out。println(“歡迎學習泡泡的分享,繼續加油哦~”);
// }
//while迴圈的死迴圈
// while(true){
// System。out。println(“都到這裡啦?奧利給~”);
// }
//do-while迴圈的死迴圈
do{
System。out。println(“相信我們可以的~”);
}while(true);
}
}
練習題5
雞兔同籠問題(窮舉法)
已知:雞兔共35只,共94只腳,那麼雞和兔各幾隻?
package cn。cxy。exec;
//窮舉法
//雞 兔
//0 35
//1 34
//2 33
//3 32
//。。。
//23 12
//。。。
//35 0
public class SameCage {
public static void main(String[] args) {
//迴圈變數j,控制小雞的個數: 0到35遞增
//迴圈變數t,控制兔子的個數: 35到0遞減
for(int j=0,t=35; j<=35; j++,t——) {//如果有多個小條件,用逗號隔開
//保證腳的數量是94
if(j*2 + t*4 == 94) {
System。out。println(“雞:”+j+“, 兔:”+t);
}
}
}
}
練習題6
求數字階乘(for迴圈版)
需求:接收使用者輸入的數字,計算該數字的階乘結果
已知:負數不可以有階乘,0的階乘結果是1,
5 ! = 5 x 4 x 3 x 2 x 1
package cn。cxy。exec;
import java。util。Scanner;
public class Factorial {
public static void main(String[] args) {
System。out。print(“輸入整數,求階乘:”);
int n = new Scanner(System。in)。nextInt();
//呼叫f()方法,把n的值傳遞到f()方法,求階乘
f(n);
}
public static void f(int n) {
if(n<0) {
System。out。println(“負數不可以求階乘”);
return;//方法結束,返回到呼叫位置繼續執行
}
if(n == 0) {
System。out。println(“0的階乘是1”);
return;
}
/*
* r = 5
* i
* 4, r=r*i
* 3, r=r*i
* 2, r=r*i
* 1, r=r*i
*/
long r = n;
for(int i=n-1; i>=1; i——) {
r *= i;
}
System。out。println(n+“的階乘:”+r);
}
}
/**其實我們還可以透過遞迴思想解決這個問題,感興趣的可以研究一下~*/
練習題7
多次生成隨機數,並列印第一次出現大於0.999 時的次數與生成的隨機數
package cn。cxy。exec;
public class ForBreak {
public static void main(String[] args) {
// Math。random()可以產生[0,1)的隨機浮點數
// >0。999
//寫一個死迴圈, i變數用來計次
for(int i=1; ;i++) {
double d = Math。random();
if(d>0。999) {
System。out。println(“第”+i+“次產生了目標值,值為:”+d);
break;
}
}
}
}
練習題8
列印100以內除了尾數為3,5,7的所有數
package cn。cxy。exec;
public class ForContinue {
public static void main(String[] args) {
for(int i=1;i<=100;i++) {
int y = i%10;//100以內的數,透過取餘求出尾數
if(y==3 || y==5 || y==7) {
continue;//如果尾數為3 5 7 ,則跳過後面的列印,進行下一輪迴圈
}
System。out。println(i);
}
}
}
練習題9
求質數:接收使用者輸入的數字,判斷是否為質數
質數的概念:一個大於1的自然數,除了1和它自身外,不能被其他自然數整除的數叫做質數,也稱為素數
規定:1既不是質數,也不是合數
package cn。cxy。exec;
import java。util。Scanner;
public class PrimeNumber {
public static void main(String[] args) {
System。out。print(“請輸入要判斷的自然數:”);
int n = new Scanner(System。in)。nextInt();
//呼叫getPrimeNumber()方法,判斷n是否為質數
getPrimeNumber(n);
}
public static void getPrimeNumber(int n) {
if(n<2) {//此範圍內的數均不為質數
System。out。println(n+“不是質數”);
return;//結束程式
}
if(n == 2) {
System。out。println(“2是質數”);
return;
}
//在 2到 1+n開方範圍(數學理論),找能把n整除的值(這個值也稱作因子)
//如果找到可以把n整除的第三個數,那n就不是質數,反之,n為質數
double max = 1+ Math。sqrt(n);//max儲存的是查詢因子的範圍
//依次遍歷範圍內的所有數,驗證是否存在n的因子
for(int i=2; i //判斷n能不能被i整除,如果有,說明不是質數 if(n%i == 0) { System。out。println(n+“不是質數”); return; } } //如果判斷了範圍內的所有值,沒有能整除的,則說明n是質數 System。out。println(n+“是質數”); } } 練習題10 接收使用者輸入的數字,判斷在此範圍內質數的個數 package cn。cxy。exec; import java。util。Scanner; public class CountPrimeNumber { public static void main(String[] args) { System。out。println(“輸入整數n,求n內質數的數量”); int n = new Scanner(System。in)。nextInt(); count(n); } public static void count(int n) { if(n<2) { System。out。println(“沒有質數”); return; } if(n==2) { System。out。println(“有1個質數”); return; } //定義計數變數 int count = 1;//已知有一個質數 outer: //從3到n尋找質數 for(int i=3; i<=n ;i++) { //判斷i是否是質數 double max = 1+ Math。sqrt(i); for(int j=2; j if(i%j == 0) {//i被j整除,i不是質數 //跳到i++,繼續判斷下一個i值 continue outer;//跳到外部outer的位置 } } //內層j迴圈結束,i是質數 count++; } System。out。println(n+“內質數的數量:”+count); } } 練習題11 生成一個順序陣列,將這個陣列的元素打亂順序後輸出 package cn。cxy。exec; import java。util。Arrays; import java。util。Random; public class ShuffleArray { public static void main(String[] args) { //呼叫f()方法,從方法獲取一個int[]陣列 int[] a = f(); //遍歷列印陣列資料