Math方法:
package com.jzlg.lanqiao;
import javax.swing.border.MatteBorder;
public class Sum {
public static void main(String[] args) {
//Math類常用方法
//返回絕對值
System.out.println(Math.abs(-12));
//返回兩個參數的教大值
System.out.println(Math.max(3, 5));
//返回兩個參數的最小值
System.out.println(Math.min(3, 5));
//返回0.0~1.0之間的double類型的隨機數,包括0.0,但不包括1.0
System.out.println((int)(Math.random()*100));
//返回四捨五入的整數值
System.out.println(Math.round(3.6));
//平方根函數
System.out.println(Math.sqrt(4));
//冪運算
System.out.println(Math.pow(2, 3));
}
}
StringBuffer類中的方法:
package com.jzlg.lanqiao;
public class Buffer {
public static void main(String args[])
{
StringBuffer stringBuffer=new StringBuffer("Mstanford");
//將字符串序列反轉輸出
stringBuffer.reverse();
//在指定位置插入字符
stringBuffer.setCharAt(4, 't');
//移除指定的字符
stringBuffer.deleteCharAt(2);
//將StringBuffer裝換爲String類型
String str=stringBuffer.toString();
System.out.println(stringBuffer);
//Integer類的操作
Integer integer=new Integer(100);
//將Inter類型轉換爲基本類型int
int a=integer.intValue();
}
}
String類中的方法
package com.jzlg.lanqiao;
public class Str {
public static void main(String agrs[]){
int a=2012;
int b=2013;
String str="Mstanford";
//返回指定位置以後的字符串
System.out.println(str.substring(2));
//返回指定範圍的新字符串
System.out.println(str.substring(0, 2));
//將整形轉換爲字符串
String a1=String.valueOf(a);
String b1=String.valueOf(b);
System.out.println(a1+b1);
//比較兩個字符串大小,下面返回-1
String str1="a";
String str2="b";
System.out.println(str1.compareTo(str2));
//返回指定位置的char值
char str3=str.charAt(2);
//查看字母的ASCII碼值,將char轉換成int
int B ='a';
int con=str3;
//將小寫字母轉換爲大寫
char con1=(char) (B-32);
System.out.println(con1);
System.out.println(B);
//測試字符串是否以指定的後綴結束,返回boolean類型
System.out.println(str.endsWith("d"));
//返回指定字符的下標
System.out.println(str.indexOf("a"));
//清除字符串左右兩端的空格
System.out.println(str);
System.out.println(str.trim());
}
}