写着玩的练习1

本文精选了多个实用的Java编程技巧,包括冒泡排序算法、字符串截取、生产者消费者模式实现、生成乘法表、素数判断、回文数检测、Date对象操作及一个保证汉字完整性的字符串截取函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.冒泡

[java] view plain copy
  1. staticvoidsort(int[]arry)
  2. {
  3. for(inti=0;i<arry.length-1;i++)
  4. {
  5. //System.out.println("lentgh=>"+arry.length);
  6. if(arry[i]<arry[i+1])
  7. {
  8. inttemp=0;
  9. //System.out.println("[i]=>"+arry[i]);
  10. //System.out.println("[i]+1=>"+arry[i+1]);
  11. temp=arry[i];
  12. arry[i]=arry[i+1];
  13. arry[i+1]=temp;
  14. i=-1;//从新检查排序,自加后归零
  15. }
  16. }
  17. }


2.按规定的个数,给字符串截取成若干组

[java] view plain copy
  1. Stringtest="123156498478645614651";
  2. for(inti=0;i<test.length();i+=5)
  3. {
  4. System.out.println(test.substring(i,(i+5>test.length()?test.length():i+5)));
  5. }

3.生产者消费者

[java] view plain copy
  1. publicclassPer_Cer{
  2. publicstaticvoidmain(String[]args){
  3. CangKuck=newCangKu(5);
  4. Pp=newP(ck);
  5. Cc=newC(ck);
  6. Threadt1=newThread(p);
  7. Threadt2=newThread(c);
  8. t1.start();
  9. t2.start();
  10. }
  11. }
  12. classCangKu
  13. {
  14. publicintnum=0;
  15. booleanisEpt=true;
  16. staticObjectl=newObject();
  17. publicCangKu(intn)
  18. {
  19. this.num=n;
  20. }
  21. publicsynchronizedvoidput(inti)
  22. {
  23. //System.out.println(Thread.currentThread()+"put"+i);
  24. //synchronized(l)
  25. {
  26. if(!isEpt)
  27. {
  28. try{
  29. this.wait();
  30. }catch(InterruptedExceptione){
  31. //TODOAuto-generatedcatchblock
  32. e.printStackTrace();
  33. }
  34. }
  35. num=i;
  36. isEpt=false;
  37. System.out.println(Thread.currentThread()+"=>put"+i);
  38. notifyAll();
  39. }
  40. }
  41. publicsynchronizedintget()
  42. {
  43. //System.out.println(Thread.currentThread()+"get");
  44. //synchronized(l)
  45. {
  46. if(isEpt)
  47. {
  48. try{
  49. this.wait();
  50. }catch(InterruptedExceptione){
  51. //TODOAuto-generatedcatchblock
  52. e.printStackTrace();
  53. }
  54. }
  55. isEpt=true;
  56. System.out.println(""+Thread.currentThread()+"=>get"+num);
  57. notifyAll();
  58. returnnum;
  59. }
  60. }
  61. }
  62. classPimplementsRunnable{
  63. publicCangKuck=null;
  64. publicP(CangKuc)
  65. {
  66. this.ck=c;
  67. }
  68. @Override
  69. publicvoidrun(){
  70. while(true)
  71. {//
  72. //System.out.println("pppppppppp");
  73. ck.put((int)(Math.random()*100));
  74. try{
  75. Thread.sleep((int)(Math.random()*2000));
  76. }catch(InterruptedExceptione){
  77. //TODOAuto-generatedcatchblock
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. }
  83. classCimplementsRunnable{
  84. publicCangKuck=null;
  85. publicC(CangKuc)
  86. {
  87. this.ck=c;
  88. }
  89. @Override
  90. publicvoidrun(){
  91. while(true)
  92. {
  93. //System.out.println("cccccccccccccc"+);
  94. ck.get();
  95. try{
  96. Thread.sleep((int)(Math.random()*2000));
  97. }catch(InterruptedExceptione){
  98. //TODOAuto-generatedcatchblock
  99. e.printStackTrace();
  100. }
  101. }
  102. }
  103. }



1:用单层for循环控制输出乘法表

		//单层for, 当行列数相等时换行
		for(int i=1,j=1; j<=9;i++)
		{
			System.out.print(" "+i*j+" ");
			if(i == j) //行列号相等时, 就该换行了
			{
				i=0;
				j++;
				System.out.println();
			}
		}


2.素数中用"开方" 缩小循环范围

	public static boolean isP(int i)
	{
		
		if(i==0 || i==1 )//一:2是素数,不用剔除出去
		{
			return false;
		}
		
		double iii = Math.sqrt(i);//二:取平方根Math.sqrt(i), 且应该是<= 比如i为4
		for(int j=2;j<=iii;j++) //三:写在for里边影响效率;j<=(Math.sqrt(i)); 写在外边更好      
		{
						
			if(i%j==0)
			{
				return false;
			}
			
		}
		
		return true;
		
	}


3.回文数 ( ABCCBA)

	public static boolean isH(int n)
	{
		if( n == revsNum(n) ) //原参数 是否等于反转后的参数
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static int revsNum(int n)
	{
		int old = n;
		int tar = 0;
		
		while(old > 0) //这个比较有意思, 用整除10的方式筛选出最低位数字
		{
			tar *= 10; //相当于把上次筛选出来的数字左移
			tar += old % 10;//加上新筛选出的最低位
			
			old /= 10;//原数字可以降低位数了
		}
		
		//System.out.println("tar=>"+tar);
		return tar;	
	}


4.Date对象的操作

Date dd =new Date(d.getTime()  + 1000*60*60*24); //getTime返回计算机时间


5.

要求:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。

1./**
2. * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
3. * 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,
4. * 应该输出为"我ABC"而不是"我ABC+汉的半个"。
5. */
6. public static String subString(String str,int len){
7. if(str == null && "".equals(str)){
8. return null;
9. }
10. //将字符串中的char数组转换成指定编码方式的byte数组的函数
11. byte[] strBytes = null;
12. try {
13. strBytes = str.getBytes("GBK");
14.
15. } catch (UnsupportedEncodingException e) {
16. e.printStackTrace();
17. }
18. //得到字符串的长度,判断截取字符串的长度是否在判断的范围内,否则返回原串
19. int strLen = strBytes.length;
20. if(len >= strLen || len < 1){
21. return str;
22. }
23.// System.out.println("strBytes.length="+strBytes.length);
24.// System.out.println("len="+len);
25. int count = 0;
26. for(int i=0; i<len; i++){
27. //将每个字节数组转换为整型数,以为后面根据值的正负来判断是否为汉字
28. int value = strBytes[i];
29.// System.out.print(value+",");
30. //如果是汉字(负),则统计截取字符串中的汉字所占字节数
31. if(value < 0){
32. count++;
33. }
34.// System.out.println("zh count="+count);
35. }
36. //依据判断给定的字符串是否含有汉字,利用String类的substring()方法来截取不同的长度
37.
38. //根据所统计的字节数,判断截取到字符是否为半个汉字,奇数为半个汉字
39. if(count % 2 !=0){
40. //如果在截取长度为1时,则将该汉字取出,
41. //其他情况则不截取这里的截取长度则按字符长度截取(截取字节长度数-截取汉字字节数/2-截取到的半个汉字的字节数)
42. len = (len == 1)?len:len-count/2-1;
43.// System.out.println("处理后的len="+len);
44.
45. }else{
46. //截取字符长度为字节长度-汉字所占字节长度/2(汉字占两个字节)
47. len = len-(count/2);
48. }
49. return str.substring(0,len);
50.
51. }
52. public static void main(String[] args) {
53. //情况一:
54. String inStr = "我ABC你";
55. String str = subString(inStr, 6);
56. System.out.println(str); //我ABC
57.
58. //情况二:首字符为汉字
59. inStr = "我ABC汉DEF";
60. str = subString(inStr, 1);
61. System.out.println(str); //我
62.
63. //情况三:中间有连续汉字
64. inStr = "我AB爱孩子CDEF";
65. str = subString(inStr,9);
66. System.out.println(str); //我AB爱孩
67.
68. //情况四:没有汉字
69. inStr = "ABCDEF";
70. str = subString(inStr,4);
71. System.out.println(str); //ABCD
72. }













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值