簡單做個筆記,就不排版了
數學方法:
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;
}
}