HOW2J,java部分习题与方法

简单做个笔记,就不排版了

数学方法:

package zifu;

public class math {
	public static void main(String[] args) {
		
		float f1 = 5.4f;
		float f2 = 5.5f;
		//四舍五入
		System.out.println(Math.round(f1));
		System.out.println(Math.round(f2));
		//得到一个0-1的随机数
		System.out.println(Math.random());
		//得到一个1-10的随机数
		System.out.println(Math.random()*10);
		//开方
		System.out.println(Math.sqrt(9));
		//次方
		System.out.println(Math.pow(2,4));
		//Π
		System.out.println(Math.PI);
		//自然常数
		System.out.println(Math.E);
		
		
		
	}
}

整型

package bianliang;

public class zx {
    long val = 26L; //以L结尾的字面值表示long型
    int decVal = 26; //默认就是int型
    int hexVal = 0x1a; //16进制
    int oxVal = 032; //8进制
    int binVal = 0b11010; //2进制
    System.out.println(oxVal);
}

浮点

package bianliang;

public class fd {
	float f1 = 123.4F;// 以F结尾的字面值表示float类型
    double d1 = 123.4;// 默认就是double类型
    double d2 = 1.234e2;// 科学计数法表示double
}

 

字符

package zifu;

public class Char {
    public static void main(String[] args) {
        
        System.out.println(Character.isLetter('a'));//判断是否为字母
        System.out.println(Character.isDigit('a')); //判断是否为数字
        System.out.println(Character.isWhitespace(' ')); //是否是空白
        System.out.println(Character.isUpperCase('a')); //是否是大写
        System.out.println(Character.isLowerCase('a')); //是否是小写
         
        System.out.println(Character.toUpperCase('a')); //转换为大写
        System.out.println(Character.toLowerCase('A')); //转换为小写
 
       // String a = 'a'; //不能够直接把一个字符转换成字符串
        String a2 = Character.toString('a'); //转换为字符串
        
        //转义符
        System.out.println("使用空格无法达到对齐的效果");
        System.out.println("abc def");
        System.out.println("ab def");
        System.out.println("a def");
          
        System.out.println("使用\\t制表符可以达到对齐的效果");
        System.out.println("abc\tdef");
        System.out.println("ab\tdef");
        System.out.println("a\tdef");
         
        System.out.println("一个\\t制表符长度是8");
        System.out.println("12345678def");
          
        System.out.println("换行符 \\n");
        System.out.println("abc\ndef");
 
        System.out.println("单引号 \\'");
        System.out.println("abc\'def");
        System.out.println("双引号 \\\"");
        System.out.println("abc\"def");
        System.out.println("反斜杠本身 \\");
        System.out.println("abc\\def");
         
    }
}

操纵字符串:

harAt

获取字符

 
toCharArray

获取对应的字符数组

 
subString

截取子字符串

 
split

分隔

 
trim

去掉首尾空格

 
toLowerCase
toUpperCase

大小写

 
indexOf
lastIndexOf
contains

定位

 
replaceAll 
replaceFirst

替换

equals:比较字符串内容

package zifu;

public class lx4 {
	public static void main(String[] args) {
	String str1 = "admlzqq";
	String str2 = new String(str1);
	String str3 =str2.toUpperCase();
	System.out.println(str1.equals(str2));
	System.out.println(str1.equals(str3));
	
	}
}

是否以字符串开始或结束

package zifu;

public class lx4 {
	public static void main(String[] args) {
	String str1="the light";
	String start ="the";
	String end ="light";
	System.out.println(str1.startsWith(start));//以the开始
	System.out.println(str1.endsWith(end));//以light结束
	}
}

字符串练习,每个太小,所以写在一块了

package zifu;

public class lx3 {
	public static void main(String[] args) {
		
		//练习1 首字母大写
	String str = "let there be light";
	char[] ch = str.toCharArray();
	ch[0]-=32;
	for(int i=0;i<ch.length;i++) {
		if(ch[i]==' ') 
			ch[i+1]-=32;
		}
	String s = String.copyValueOf(ch);
	System.out.println(s);
	
		//练习2 间隔大小写
	
	String str2="lengendary";
	char[] ch2 = str2.toCharArray();
	for(int i=0;i<str2.length();i++) {
		if(i%2==0)
			ch2[i]-=32;
	}
		String s2=String.copyValueOf(ch2);
		System.out.println(ch2);
		
		//练习三最后一个字母变大写
		char[] ch3 =str2.toCharArray();
		ch3[ch3.length-1]-=32;
		String s3 =String.copyValueOf(ch3);
		System.out.println(ch3);
		//练习四 最后一个two首字母大写
		String str4="Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
		char[] ch4 = str4.toCharArray();
		ch4[str4.lastIndexOf("two")]-=32;
		String s4 = String.copyValueOf(ch4);
		System.out.println(s4);

	}

}

比较字符串

//题目要求
//创建一个长度是100的字符串数组/
//使用长度是2的随机字符填充该字符串数组
//统计这个字符串数组里重复的字符串有多少种
package zifu;

public class lx4 {
	public static void main(String[] args) {
	String[] str =new String[100];
	for(int i=0;i<str.length;i++) {
		str[i]=randomString(2);
	
	}
	//System.out.println(str[0]+str[1]);
	int count=0;
	String[] sz=new String[10];
	int k=0;
	for(int i=0;i<str.length-1;i++) {
		for(int j=i+1;j<str.length;j++) {
			if(str[i].equals(str[j])) {
				//System.out.println(str[i]);
				sz[k]=str[i];
				k++;
				count++;
			}
		}
	}
	System.out.format("有%d个字符串重复,分别是\n",count);
	for(int i=0;i<sz.length&&sz[i]!=null;i++) {
		System.out.println(sz[i]);
	}
	
	}
	public static String randomString(int length) {
		String str="";
		for( short i='a';i<'z';i++) {
			str+=(char)i;}
		for(short i='A';i<'Z';i++) {
			str+=(char)i;}
	
		char[] cs =new char[length];
		for(int i=0;i<cs.length;i++) {
			int index =(int)(Math.random()*str.length());
			cs[i]=str.charAt(index);
		}
		String result =new String(cs);
		return result;
		
	}
}

StringBuff用法及例题

package zifu;
import java.util.*;
public class lx3 {
	public static void main(String[] args) {
		
//		String str = "let there";
//		//StringBuff 是可变长的字符串
//		StringBuffer strb = new StringBuffer(str);
//		System.out.println(strb);
//		//在最后追加
//		strb.append(" be light");
//		System.out.println(strb);
//		//删除4-10之间的字符
//		strb.delete(4,10);
//		System.out.println(strb);
//		//在4的位置插入there
//		strb.insert(4,"there ");
//		System.out.println(strb);
//		//反转
//		strb.reverse();
//		System.out.println(strb);
//		
		double t1=System.currentTimeMillis();
		System.out.println(t1);
		String str ="";
		StringBuffer sb= new StringBuffer(str);
		for(int i=0;i<1000;i++) {
			//String s =randomString(10);
			//str+=s;//35毫秒
			sb.append(randomString(10));//15毫秒
		}
		
		double t2 =System.currentTimeMillis();
		
		System.out.println(t2-t1);
			
	}
	public static String randomString(int length) {
		String str="";
		for( short i='a';i<'z';i++) {
			str+=(char)i;}
		for(short i='A';i<'Z';i++) {
			str+=(char)i;}
	
		char[] cs =new char[length];
		for(int i=0;i<cs.length;i++) {
			int index =(int)(Math.random()*str.length());
			cs[i]=str.charAt(index);
		}
		String result =new String(cs);
		return result;
		
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值