java實戰練習


1.請根據控制台輸入的特定日期格式拆分日期

如:請輸入一個日期(格式如:**月**日****年)經過處理得到:****年**月**日

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num1 {
 6     public static void main(String[] args) {
 7 
 8         Scanner c = new Scanner(System.in);
 9         System.out.println("請輸入日期:(格式如:**月**日****年)");
10         String str = c.nextLine();
11         int num = str.indexOf("日");
12         String str1 = str.substring(num + 1);
13         String str2 = str.substring(0, num + 1);
14         System.out.println(str1 + str2);
15     }
16 }

2.給出一個隨機字符串,判斷有多少字母?多少數字?

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 import javax.xml.stream.events.Characters;
 6 
 7 public class num2 {
 8     public static void main(String[] args) {
 9         Scanner c = new Scanner(System.in);
10         System.out.println("請輸入字符串:");
11         String str = c.nextLine();
12         int n = str.length();
13         int num = 0, let = 0;
14         for (int i = 0; i < n; i++) {
15             if (Character.isDigit(str.charAt(i))) {
16                 num++;
17             }
18             if (Character.isLetter(str.charAt(i))) {
19                 let++;
20             }
21         }
22         System.out.println("字母:" + let + '\n' + "數字:" + num);
23     }
24 }

3.統計段落中出現某個詞的次數

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num3 {
 6     public static void main(String[] args) {
 7         Scanner c = new Scanner(System.in);
 8         int count = 0;
 9         System.out.println("請輸入句子:");
10         String str = c.nextLine();
11         System.out.println("請輸入要查找的詞:");
12         String card = c.next();
13         count = word(str, card);
14         System.out.println(card + "在句子中出現" + count);
15     }
16 
17     public static int word(String str, String card) {
18         int count = 0, index = 0;
19         while ((index = str.indexOf(card, index)) != -1) {
20             index = index + card.length();
21             count++;
22         }
23         return count;
24     }
25 }

4.編寫敏感詞過濾程序

說明:在網絡程序中,如聊天室、聊天軟件等,經常需要對一些用戶所提交的聊天內容中的敏感性詞語進行過濾。

如“性”、“色情”、“爆炸”、“恐怖”、“槍”、“軍火”等,這些都不可以在網上進行傳播,需要過濾掉或者用其他詞語替換掉。

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num4 {
 6     public static void main(String[] args) {
 7         Scanner c = new Scanner(System.in);
 8         System.out.println("請輸入聊天內容:");
 9         String str = c.nextLine();
10         //StringBuilder str1 = new StringBuilder(str);
11         String  ch[] = { "色情", "爆炸", "恐怖", "槍", "軍火" };
12         for (String s : ch) {
13             str = str.replaceAll(s, "*");
14         }
15         System.out.println("聊天內容:" + str);
16     }
17 }

5.根據輸入的年份、產品類型和隨機數產生固定資產編號    

即:固定資產編號=年份+0+產品類型+3位隨機數    

程序運行流程:

請輸入年份:                  
請選擇產品類型(1.台式機 2.筆記本 3.其他):            

生成3位隨機數    最后顯示固定資產編號

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num5 {
 6     public static void main(String[] args) {
 7         Scanner c = new Scanner(System.in);
 8         System.out.println("請輸入年份:");
 9         String str = c.nextLine();
10         System.out.println("請輸入產品類型:");
11         int a = c.nextInt();
12         int b = (int) (Math.random() * 1000);
13         switch (a) {
14         case 1:
15             System.out.println("固定資產編號:" + str + 0 + "台式機" + b);
16             break;
17         case 2:
18             System.out.println("固定資產編號:" + str + 0 + "筆記本" + b);
19             break;
20         case 3:
21             System.out.println("固定資產編號:" + str + 0 + "其他" + b);
22             break;
23         }
24     }
25 }

6.計算某年、某月、某日和某年、某月、某日之間的天數間隔和周數。

 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import java.util.Scanner;
 7 
 8 public class num6 {
 9     public static void main(String[] args) throws ParseException {
10         Scanner c = new Scanner(System.in);
11         System.out.println("請輸入第一個日期:");
12         String str = c.nextLine();
13         System.out.println("請輸入第二個日期:");
14         String str1 = c.nextLine();
15         SimpleDateFormat str2 = new SimpleDateFormat("yyyy-MM-dd");
16         Date x = str2.parse(str);
17         Date x1 = str2.parse(str1);
18         long a = 0;
19         if (x.after(x1)) {
20             a = x.getTime() - x1.getTime();
21         } else {
22             a = x1.getTime() - x.getTime();
23         }
24         long d = 1000 * 60 * 60 * 24;
25         long day = a / d;
26         long week = day / 7;
27         System.out.println("兩個日期相隔" + day + "天");
28         System.out.println("相隔" + week + "周");
29     }
30 }

7.計算並輸出21世紀的閏年,計算程序的執行時間。

 1 package test;
 2 
 3 import java.util.GregorianCalendar;
 4 
 5 public class num7 {
 6     public static void main(String[] args) {
 7         long start = System.currentTimeMillis();
 8         GregorianCalendar g = new GregorianCalendar();
 9         for (int i = 2000; i < 2100; i++) {
10             if (g.isLeapYear(i)) {
11                 System.out.println(i + "是閏年");
12             }
13         }
14         long end = System.currentTimeMillis();
15         System.out.println("程序的執行時間為:" + (end - start) + "毫秒");
16     }
17 }

8.編寫一個程序,設定一個有大小寫字母的字符串,先將字符串的大寫字符輸出,再將字符串中的小寫字符輸出。

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class num8 {
 6     public static void main(String[] args) {
 7         Scanner c = new Scanner(System.in);
 8         System.out.println("請輸入字符串:");
 9         String str = c.nextLine();
10         char ch[] = str.toCharArray();
11         System.out.println("大寫字母為:");
12         for (char a : ch) {
13             if (Character.isUpperCase(a)) {
14                 System.out.println(a);
15             }
16         }
17         System.out.println("小寫字母為:");
18         for (char a : ch) {
19             if (Character.isLowerCase(a)) {
20                 System.out.println(a);
21             }
22         }
23     }
24 }

9.編寫程序,當以年-月-日的格式輸入一個日期時,輸出其該年是否為閏年,該月有幾天,該日是星期幾

 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Calendar;
 6 import java.util.Date;
 7 import java.util.GregorianCalendar;
 8 import java.util.Scanner;
 9 
10 public class num9 {
11     public static void main(String[] args) throws ParseException {
12         Scanner c = new Scanner(System.in);
13         System.out.println("請輸入日期:(格式:年-月-日)");
14         String str = c.nextLine();
15         SimpleDateFormat str1 = new SimpleDateFormat("yyyy-MM-dd");
16         Date d = str1.parse(str);
17         Calendar a = Calendar.getInstance();
18         a.setTime(d);
19         int year = a.get(Calendar.YEAR);
20         int month = a.get(Calendar.MONTH)+1;
21         int week = a.get(Calendar.DAY_OF_WEEK)-1;
22         GregorianCalendar g = new GregorianCalendar();
23         if (g.isLeapYear(year)) {
24             System.out.println(year + "是閏年");
25         } else {
26             System.out.println(year + "是平年");
27         }
28         int max = a.getActualMaximum(Calendar.DAY_OF_MONTH);
29         String s = week == 0 ? "周日" : "周" + week;
30         System.out.println(month + "月有" + max + "天,該日是" + s);
31     }
32 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM