Math类:
Math.abs() 返回参数的绝对值
public class JunitMath {
public static void main(String[] args) {
double number=-1.0; //双精度
int num=-1; //整数
double result;
int re;
result=Math.abs(number); //双精度取绝对值
re=Math.abs(num); //整数取绝对值
System.out.println("双精度"+number+"绝对值为:"+result);
System.out.println("整数"+num+"绝对值为:"+re);
}
}
执行结果:
Math.ceil() 向上取整,双精度
public class JunitMath {
public static void main(String[] args) {
double number=1.56; //正数
double num=-1.56; //负数
double result=Math.ceil(number); //正数向上取整
double re=Math.ceil(num); //负数向上取整
System.out.println("正数"+number+"向上取整的结果为:"+result);
System.out.println("负数"+num+"向上取整的结果为:"+re);
}
}
执行结果:
Math.floor() 向下取整
public class JunitMath {
public static void main(String[] args) {
double number=2.35; //正数
double num=-2.35; //负数
double result;
double re;
result=Math.floor(number); //正数向下取整
re=Math.floor(num); //负数向下取整
System.out.println("正数"+number+"向下取整为:"+result);
System.out.println("负数"+num+"向下取整为:"+re);
}
}
执行结果:
Math.rint() 返回与参数最接近的整数,双精度
public class JunitMath {
public static void main(String[] args) {
double number=2.67; //正数
double num=-2.35; //负数
double result;
double re;
result=Math.rint(number); //正数返回与参数最接近的整数
re=Math.rint(num); //负数返回与参数最接近的整数
System.out.println("与正数"+number+"最接近的整数为:"+result);
System.out.println("与负数"+num+"最接近的整数为:"+re);
}
}
执行结果:
Math.round() 四舍五入
public class JunitMath {
public static void main(String[] args) {
double number=2.55; //正数
double num=-2.22; //负数
double result;
double re;
result=Math.round(number); //正数四舍五入
re=Math.round(num); //负数四舍五入
System.out.println("正数"+number+"四舍五入为:"+result);
System.out.println("负数"+num+"四舍五入为:"+re);
}
}
执行结果:
Math.exp() 返回自然底数e的参数次方
public class JunitMath {
public static void main(String[] args) {
double number=2.55; //正数
double num=-2.22; //负数
double result;
double re;
result=Math.exp(number); //正数自然底数e的参数次方
re=Math.exp(num); //负数自然底数e的参数次方
System.out.println("正数"+number+"自然底数e的参数次方为:"+result);
System.out.println("负数"+num+"自然底数e的参数次方为:"+re);
}
}
执行结果:
Math.log() 返回参数的自然底数的对数值
public class JunitMath {
public static void main(String[] args) {
double number=5; //正数
double result;
double re;
result=Math.log(number); //正数参数的自然底数的对数值
System.out.println("正数"+number+"的自然底数的对数值为:"+result);
}
}
执行结果:
Math.pow() 返回第一个参数的第二个参数次方
public class JunitMath {
public static void main(String[] args) {
int x=5,y=2;
double result;
result=Math.pow(x,y); //x的y次方
System.out.println(x+"的"+y+"次方为:"+result);
}
}
执行结果:
Math.sqrt() 求参数的算术平方根
public class JunitMath {
public static void main(String[] args) {
int x=25;
double result;
result=Math.sqrt(x); //x的算术平方根
System.out.println(x+"的算术平方根为:"+result);
}
}
执行结果:
Math.sin() 求指定double类型参数的正弦值
public class JunitMath {
public static void main(String[] args) {
double x=60;
double result;
result=Math.sin(x); //求x的正弦值
System.out.println(x+"的正弦值为:"+result);
}
}
执行结果:
Math.cos() 求指定double类型参数的余弦值
public class JunitMath {
public static void main(String[] args) {
double x=45;
double result;
result=Math.cos(x); //求x的余弦值
System.out.println(x+"的余弦值为:"+result);
}
}
执行结果:
Math.tan() 求指定double类型参数的正切值
public class JunitMath {
public static void main(String[] args) {
double x=0;
double result;
result=Math.cos(x); //求x的正切值
System.out.println(x+"的正切值为:"+result);
}
}
执行结果:
Math.random() 返回一个随机数
public class JunitMath {
public static void main(String[] args) {
double result;
result=Math.random(); //生成随机数
System.out.println("生成随机数:"+result);
}
}
执行结果:
String类:
matches(String regex) 告知此字符串是否匹配给定的正则表达式。返回指定索引处的char值
public class JunitString {
public static void main(String[] args) {
String str="test001";
if(str.matches("^[a-zA-Z0-9]*$")) { //判定str是否匹配给定的正则表达式
System.out.println("True");
}else {
System.out.println("False");
}
}
}
执行结果:
concat(String str) 将指定字符串连接到此字符串的结尾
public class JunitString {
public static void main(String[] args) {
String str_001="test001";
String str_002="test002";
String result=str_001.concat(str_002);
System.out.println(str_001+"与"+str_002+"连接的结果为:"+result);
}
}
执行结果:
endWith(String suffix) 测试此字符串是否以指定的后缀结束
public class JunitString {
public static void main(String[] args) {
String str_001="the first test";
if(str_001.endsWith("test")) { //判断str_001是否以"test"的后缀结束
System.out.println("True");
}else {
System.out.println("False");
}
}
}
执行结果:
equals(Object anObject) 将此字符串与指定的对象比较
public class JunitString {
public static void main(String[] args) {
String str_001="test equals";
if(str_001.equals("test equals")) { //判断str_001是否与"test equals"相同
System.out.println("True");
}else {
System.out.println("False");
}
}
}
执行结果:
length() 返回此字符串的长度
public class JunitString {
public static void main(String[] args) {
String str_001="test length";
int result=str_001.length(); //测str_001字符串的长度
System.out.println(result);
}
}
执行结果:
replace(char oldChar,char newChar) 用newChar替换oldChar
public class JunitString {
public static void main(String[] args) {
String str_001="oldChar";
String result=str_001.replace("old", "new");
System.out.println(str_001+"替换后的字符串为:"+result);
}
}
执行结果:
replaceAll(String regex,String replacement) 用正则表达式替换此字符串匹配的字符串
public class JunitString {
public static void main(String[] args) {
String str_001="oldchar001newchar002";
String result=str_001.replaceAll("\\d", "f"); //将数字替换为'f'
System.out.println(str_001+"\n正则表达式替换'test'匹配的字符串为:\n"+result);
}
}
执行结果:
replaceFirst(String regex,String replacement) 用正则表达式替换此字符串匹配的第一个字符串
public class JunitString {
public static void main(String[] args) {
String str_001="oldchar001newchar002";
String result=str_001.replaceFirst("\\d", "f"); //将第一个数字替换为'f'
System.out.println(str_001+"\n正则表达式替换'test'匹配的字符串为:\n"+result);
}
}
执行结果:
startsWith(String prefix) 测试此字符串是否以指定的前缀开始
public class JunitString {
public static void main(String[] args) {
String str_001="test the first";
if(str_001.startsWith("test")) { //判断str_001是否以"test"的前缀开始
System.out.println("True");
}else {
System.out.println("False");
}
}
}
执行结果:
toLowerCase() 将字符串转换为小写
public class JunitString {
public static void main(String[] args) {
String str_001="toLOWERcase";
String result=str_001.toLowerCase(); //将字符串转换为小写
System.out.println(str_001+"转换为小写为:\n"+result);
}
}
执行结果:
toUpperCase() 将字符串转换为大写
public class JunitString {
public static void main(String[] args) {
String str_001="TOuppercase";
String result=str_001.toUpperCase(); //将字符串转换为大写
System.out.println(str_001+"转换为大写为:\n"+result);
}
}
执行结果:
isEmpty() 判断字符串是否为空
public class JunitString {
public static void main(String[] args) {
String str_001="test";
String str_002="";
if(str_001.isEmpty()) { //判定str_001是否为空
System.out.println(str_001+" is Empty");
}else {
System.out.println(str_001+" is not Empty");
}
if(str_002.isEmpty()) { //判定str_002是否为空
System.out.println(str_002+"is Empty");
}else {
System.out.println(str_002+"is not Empty");
}
}
}
执行结果: