1. 程式人生 > 實用技巧 >switch語句

switch語句

switch語句中,必須是整型(不可以是小數點型別,也就是1,2,3,4,5),字元型,字串型別,遇到break才能停止,否則會一直執行

import java.util.Arrays;
import java.util.Date;

/**
* @auther 付強
* @date 2020/7/21 - 5:37
*/
public class java5 {
public static void main(String[] args) {
if(3>5){
System.out.println(" 我是dj");
}else if(3==5){
System.out.println(" 你也是dj");
}else{
System.out.println("社會人的毒打");
}
String str="白菜";
switch (str){
case "白菜":
System.out.println("我是一個大白菜");
break;
case "胡蘿蔔":
System.out.println("我是一個胡蘿蔔");
break;
default:
System.out.println("我什麼都不是");
}
Loop:
for(int i=0;i<100;i++){
for (int j = 0; j < 500; j++) {
if(j%2==0){
continue;
}if(j==300){
break;
}if(j==200){
break Loop;
}
}
}
int k=3;
while (k<500){
System.out.println("我是白菜");
if(k==5){
break;
}
k++;
}
//string常用的構造方法
char[] a={'g','o','o','d'};
String s=new String(a);
System.out.println(s);
char[] b={'s','t','u','d','e','n','t'};
String e=new String(b,2,4);
System.out.println(e);
String n=new String("good");
//連線多個字串的方法
String str1="hello";
String str2="world";
int num=65;
double num2=12.65;
System.out.println(str1+" "+str2+" "+num+" "+"小時看書"+num2);
str1.length();//str1的長度
str1.charAt(3);//str1中索引是5的字元
str1.indexOf('w');//w第一次所出現的索引位置
str1.lastIndexOf('d');//d最後一次出現的索引位置
str1.indexOf("w");
str1.lastIndexOf("d");
str1.substring(3);//表示從索引3開始截取出來新的字串,形成新的字串,包頭
str1.substring(3,5);//從索引3切到索引5,包頭不包尾
str1.trim();//去除空格
str1.trim().length();//去除空格後的長度
str1.replace("e","m");//將e替換成m
str1.replaceAll("e","m");//將所有e都替換成m
boolean l=str1.startsWith("a");//判斷字串str1是否以a開頭,是返回true,不是false
boolean l1=str1.endsWith("a");//判斷字串str1是否以a結尾,是返回true,不是false
str1.equals(str2);//判斷str1和str2是否相同,相同返回true,不同返回false
str1.equalsIgnoreCase(str2);//判斷str1和str2是否相同,不區分大小寫
str1.compareTo(str2);//比較兩者的unicode編碼,在前就為負數,在後就為正數
str1.toLowerCase();//轉成小寫
str1.toUpperCase();//轉成大寫
String str3="192.168.0.0.1";
// 用這種方式分割需要新增轉義字元,才可以進行分割
String[] split = str3.split("\\.");//利用來進行分割
System.out.println(Arrays.toString(split));
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
for(String split1:split){
System.out.println("["+split1+"]");
}
Date date=new Date();
String tY = String.format("%tY", date);//年
String tB = String.format("%tB", date);//月
String td = String.format("%td", date);//日
String tH = String.format("%tH", date);//小時
String tM = String.format("%tM", date);//分鐘
String tS = String.format("%tS", date);//秒
String time = String.format("%tc", date);//全部時間
String form = String.format("%tF", date);//全部時間
System.out.println(tY);
System.out.println(tB);
System.out.println(td);
System.out.println(tH);
System.out.println(tM);
System.out.println(tS);
System.out.println(time);
System.out.println(form);
//.匹配任意字元
//\d 0-9任意數字
//\D 表示任何一個非數字字元
//\s 表示空白字元
//\S 表示非空白字元
//? 0-1次
//* 0次或者多次
//+ 一次或者多次
long start1 = System.currentTimeMillis();
String sum="";
for (int i = 0; i < 10000; i++) {
sum+=i;
}
long end1 = System.currentTimeMillis();
System.out.println(end1-start1);
long start2 = System.currentTimeMillis();
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < 10000; i++) {
stringBuilder.append(i);
}
long end2 = System.currentTimeMillis();
System.out.println(end2 - start2);
}
}