如果是剛接觸或者剛學習java,練習一些基礎的算法還是必須的,可以提升思維和語法的使用
1、輸出兩個int數中的最大值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.scanner; public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println( "請依次輸入兩個整數:a,b(以空格隔開)" ); /*比較兩個數的大小*/ int a = scanner.nextint(); int b = scanner.nextint(); int max; if (a >= b){ max = a; } else { max = b; } system.out.println( "最大值為" +max); } } } |
2、輸出三個int數中的最大值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package demo; import java.util.scanner; public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println( "請依次輸入兩個整數:a,b(以空格隔開)" ); int a = scanner.nextint(); int b = scanner.nextint(); int c = scanner.nextint(); scanner.close(); /*方法一*/ int d=(a>b)a:b; int e=(d>c)d:c; system.out.println("最大值為"+e); /*方法二*/ if (a>b && a>c){ system.out.println( "最大值為" +a); } else if (b>c && b>a){ system.out.println( "最大值為" +b); } else if (c>b && c>a){ system.out.println( "最大值為" +c); } else { system.out.println( "出現異常" ); } } } |
3、編寫程序判斷某一個年份是否是閏年
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package demo; import java.util.scanner; /*判斷閏年 由用戶輸入任意一個年份,能被4整除但不能被100整除,或者能被400整除,是閏年。 要求判斷一個年份是否為閏年。 要求輸出:此年份是否是閏年 */ public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println("請輸入年份:"); int year = scanner.nextint(); /*方法一*/ if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){ system.out.println("這個年份是閏年"); }else{ system.out.println("這個年份不是閏年"); } /*方法二*/ boolean isleapyear = (year % 4 == 0 && year % 100 != 0 ) || year% 400 == 0 ; string string = isleapyearyear+ "是閏年" :year+ "不是閏年" ; system.out.println(string); } } |
4、完成成績等級輸出程序:如果用戶輸入的分數正確(0-100),則根據表-1中的規則計算該分數所對應的的級別,并計算結果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package demo; import java.util.scanner; /* * 成績等級劃分表 * >= 90 a * >=80 b * >=60 c * <60 d * * 分數范圍:0-100 * * 需要有2個判斷*/ public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println( "請輸入分數:" ); double score = scanner.nextdouble(); scanner.close(); if (score < 0 || score > 100 ){ system.out.println( "輸入的分數不在0-100之間,不符合要求" ); } else if (score >= 90 ){ system.out.println( "a" ); } else if (score >= 80 ){ system.out.println( "b" ); } else if (score >= 60 ){ system.out.println( "c" ); } else { system.out.println( "d" ); } } } |
5、完成命令解析程序:有一個命令解析程序,該程序提供三個功能選型供用戶選擇,用戶選擇某功能后,程序在界面上輸出用 戶所選擇的的功能名稱。程序的交互如圖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package demo; import java.util.scanner; /* * 有一個命令解析程序,該程序提供三個功能選型供用戶選擇, * 用戶選擇某功能后,程序在界面上輸出用戶所選擇的的功能名稱。 * * */ public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println( "請選擇功能:1.顯示全部記錄 2.查詢登錄記錄 0.退出" ); int command = scanner.nextint(); scanner.close(); switch (command) { case 0 : system.out.println( "歡迎使用" ); break ; case 1 : system.out.println( "顯示全部記錄……" ); break ; case 2 : system.out.println( "查詢登錄記錄……" ); break ; default : system.out.println( "輸入錯誤!" ); } } } |
6、完成收銀柜臺收款程序:編寫一個收銀柜臺收款程序,根據單價、購買數量以及收款進行計算并輸出應收金額和找零;當總價大于或者等于500,享受8折優惠。控制臺交互如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package demo; import java.util.scanner; /* * 需求: * 編寫一個收銀柜臺收款程序。根據單價、購買數量以及收款進行計算并輸出應收金額和找零; * 當總價大于或者等于500,享受8折優惠。 * */ public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println( "請輸入單價(¥):" ); double price = scanner.nextdouble(); system.out.println( "請輸入數量:" ); double amount = scanner.nextdouble(); system.out.println( "請輸入收款金額:" ); double count = scanner.nextdouble(); double totalmoney = price*amount; if (totalmoney > 500 ){ totalmoney = totalmoney* 0.8 ; } double change = count - totalmoney; system.out.println( "應收金額為:" +totalmoney + "找零為:" +change); } } |
7、java從鍵盤輸入三個整數,實現從小到大排序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package demo; import java.util.scanner; /* * java從鍵盤輸入三個整數,實現從小到大排序 * **/ public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println( "請輸入三個整數,以空格隔開:" ); int a = scanner.nextint(); int b = scanner.nextint(); int c = scanner.nextint(); scanner.close(); system.out.println( "輸入的值為:a = " + a + ", b = " + b + ", c = " + c); if (a > b){ if ( b > c) { system.out.println( "排序后的值為:" + c + "," + b + "," + a); } else if ( c > a){ system.out.println( "排序后的值為:" + b + "," + a + "," + c); } else { system.out.println( "排序后的值為:" + b + "," + a + "," + c); } } else { if (c < a){ system.out.println( "排序后的值為:" + c + "," + a + "," + b); } else if (c > b){ system.out.println( "排序后的值為:" + a + "," + b + "," + c); } else { system.out.println( "排序后的值為:" + a + "," + c + "," + b); } } } } |
8、計算個人所得稅 北京地區的個人所得稅計算公式:應納稅額 = (工資薪金所得 - 扣除數)*適用稅率 - 速算扣除數 其中,扣除數為3500,適用稅率以及速算扣除數如下表所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package demo; import java.util.scanner; /* * 北京地區的個人所得稅計算公式: 應納稅額 = (工資薪金所得 - 扣除數)*適用稅率 - 速算扣除數 其中,扣除數為3500 */ public class demo { public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println("請輸入你的稅前工資:"); int salarybeforetax = scanner.nextint(); scanner.close(); int taxsalary = salarybeforetax - 3500; double tax; /* 方法一*/ tax = taxsalary<00.0: taxsalary<=1500taxsalary*0.03: taxsalary<=4500taxsalary*0.1-105: taxsalary<=9000taxsalary*0.2-555: taxsalary<=35000taxsalary*0.25-1005: taxsalary<=55000taxsalary*0.3-2755: taxsalary<=80000taxsalary*0.35-5505: taxsalary*0.45-13505; system.out.println("個人應繳納稅款為:"+tax); /*方法二*/ if ( taxsalary < 0 ){ tax = 0 ; } else if ( taxsalary <= 1500 ){ tax = taxsalary* 0.03 ; } else if ( taxsalary <= 4500 ){ tax = taxsalary* 0.1 - 105 ; } else if ( taxsalary <= 9000 ){ tax = taxsalary* 0.2 - 555 ; } else if ( taxsalary <= 35000 ){ tax = taxsalary* 0.25 - 1005 ; } else if ( taxsalary <= 55000 ){ tax = taxsalary* 0.3 - 2755 ; } else if ( taxsalary <= 80000 ){ tax = taxsalary* 0.35 - 5505 ; } else { tax = taxsalary* 0.45 - 13505 ; } system.out.println( "個人應繳納稅款為:" +tax); } } |
9、輸入年份和月份,輸出天數。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package demo; import java.util.scanner; /* 提示: 1.需要判斷是否是閏年,2月份的天數跟是否是閏年有關系; 2.用switch-case判斷每個月的天數 */ public class demo{ public static void main(string[] args) { scanner scanner = new scanner(system.in); system.out.println("請輸入年份:"); int year = scanner.nextint(); system.out.println("請輸入月份:"); int month = scanner.nextint(); int daynum = thedaynum(month); //先根據月份得出天數,如果是閏年,對2月份的天數重新獲取 if(isleapyear(year)){ if(month == 2){ daynum ++; //如果是閏年,2月份增加一天 } system.out.print(year + "是閏年,"); }else{ system.out.print(year + "不是閏年,"); } system.out.println(year + "年" + month + "月份共有" + daynum + "天"); } /*判斷是否是閏年 * 能被4整除但不能被100整除,或者能被400整除,是閏年 */ public static boolean isleapyear(int year) { if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){ return true; }else{ return false; } } /*判斷天數*/ public static int thedaynum( int month) { switch (month) { case 1 : return 31 ; case 2 : return 28 ; case 3 : return 31 ; case 4 : return 30 ; case 5 : return 31 ; case 6 : return 30 ; case 7 : return 31 ; case 8 : return 31 ; case 9 : return 30 ; case 10 : return 31 ; case 11 : return 30 ; case 12 : return 31 ; default : system.out.println( "對不起,您輸入的月份有誤!" ); return 0 ; } } } |
10、輸出九九乘法表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package demo; /* author:wendy * 問題: * 直接輸出九九乘法表 * */ public class demo { public static void main(string[] args) { //i變量用于控制行數 for ( int i = 0 ; i <= 9 ; i++) { //j變量用于控制每行中參與計算的數值 for ( int j = 1 ; j <= i; j++) { system.out.print(j + "*" + i + "=" + i*j + "\t" ); } //每行輸出之后需要換行 system.out.println(); } } }<strong> </strong> |
11、隨機產生一個從0-100之間的整數,判斷是否是質數 質數又稱素數,是指在一個大于1的自然數中,除了1和此整數自身外,不能被其他自然數整除的數 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package demo; import java.util.random; public class primenum { public static void main(string[] args) { int num; random random = new random(); num = random.nextint( 100 ); system.out.println( "隨機產生的數為:" + num); system.out.println(isprime(num)); } public static boolean isprime( int num) { if (num < 2 ) { return false ; } if (num == 2 ) { return true ; } if (num % 2 == 0 ) { return false ; } for ( int i = 3 ; i <= math.sqrt(num); i += 2 ) { if (num % i == 0 ) { return false ; } } return true ; } } |
12、查找數組最小值,并將數組擴容成新數組。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package demo; import java.util.arrays; import java.util.random; /* * author:wendy * 問題:隨機產生10個從0-100之間的整數,并查找最小值; * 將該數組擴容成新數組,把最小值存在新數組的第一個位置。 * 步驟: * 1.構造一個長度為10的數組,利用random隨機產生10個0-100之間的整數; * 2.尋找最小值,利用for循環 * 3.擴容 利用arrays.coprof()構造新數組,將其長度設置為11 * 4.遍歷新數組,從后往前遍歷,以此賦值,然后將2中找到的最小值存在數組的第一個 * */ public class copyof { public static void main(string[] args) { int [] arr = new int [ 10 ]; //隨機產生10個 0-100之間的整數 random random = new random(); for ( int i = 0 ; i < 10 ; i ++) { arr[i] = random.nextint( 100 ); } //打印數組的內容 system.out.println( "隨機產生的數組為:" + arrays.tostring(arr)); //查找最小的值 int min = arr[ 0 ]; for ( int j = 1 ; j < 10 ; j ++) { if (min > arr[j]) { min = arr[j]; } } system.out.println( "該數組最小的值為:" + min); //擴容,將最小值存在擴容之后的第一個 int [] newarr = arrays.copyof(arr, 11 ); //從后往前遍歷,將前面的值賦給后面的值,然后將第一個的值賦為最小值min for ( int k = newarr.length- 1 ; k >= 1 ; k --) { newarr[k] = newarr[k- 1 ]; } //將第一個的值賦為最小值min newarr[ 0 ] = min; //打印數組的內容 system.out.println( "擴容之后的數組為:" + arrays.tostring(newarr)); } } |
這篇文章主要內容對初次接觸java算法和思維的學員有很大的幫助,小編會給大家提供相關幫助,請多多關注服務器之家。
原文鏈接:https://blog.csdn.net/u010297791/article/details/77367589