不经常用到的循环语法:
//while
int i = 10;
while (i > 5){
System.out.println("i = " + i);
i--;
}
//do-while
int j = 10;
do {
System.out.println("j = " + j);
j--;
}while (j>5);
// for each
int [] numbers = {10, 20, 30, 40, 50};
for (int x: numbers) {
System.out.println("x = " + x);
}
//switch case
String res = "1";
switch (res){
case "1":
System.out.println("1");break;
case "q":
System.out.println("q");break;
case "M":
System.out.println("M");break;
default:
System.out.println("no");
}
}
//while
while("判断"){
//执行语句
}
//do-while
do{
//执行语句
}while("判断");
//switch
switch(Key){
case Value1:
//action1;
break;
case Value2:
//action2
break;
default:
//action3
}
//foreach
int[] ints = new int[]{}
for(int i : ints){
//action
}
Character 方法
public static void main(String[] args){
String s = "asd12312ZDFCasd";
char[] chars = s.toCharArray();
ArrayList<String> arraysStr = new ArrayList<String>();
ArrayList<Integer> arraysInt = new ArrayList<Integer>();
for (char c : chars){
if (Character.isLowerCase(c)){
arraysStr.add(String.valueOf(Character.toUpperCase(c)));
}else if(Character.isUpperCase(c)){
arraysStr.add(String.valueOf(c));
}else if (Character.isDigit(c)){
arraysInt.add(Integer.valueOf(c));
}
}
System.out.println();
}
char转String的方法:
String.valueOf(c)
这个方法运行的arraysInt结果为ASCII码,而非12312;
那么char转Integer的方法是什么呢?
思路一,先转为String,再使用String.ParsaInt方法
String ss = "9";
Integer integer = Integer.parseInt(ss);
思路二,使用CharaCter自带的方法:
Character.getNumericValue
更新之后代码如下:
public static void main(String[] args){
String ss = "9";
Integer integer = Integer.parseInt(ss);
String s = "asd12312ZDFCasd";
char[] chars = s.toCharArray();
ArrayList<String> arraysStr = new ArrayList<String>();
ArrayList<Integer> arraysInt = new ArrayList<Integer>();
for (char c : chars){
if (Character.isLowerCase(c)){
arraysStr.add(String.valueOf(Character.toUpperCase(c)));
}else if(Character.isUpperCase(c)){
// char转String的方法
arraysStr.add(String.valueOf(c));
}else if (Character.isDigit(c)){
arraysInt.add(Character.getNumericValue(c));
}
}
System.out.println();
}