場合:
1.[単語1#単語2#単語3]
package cn.hei.string;
public class Work01StringPractise {
public static void main(String[] args) {
int[] array = {
1,2,3};
String result = fromArrayToString(array);
System.out.println(result);
}
public static String fromArrayToString(int[] array){
String str = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length-1){
str += "word"+array[i]+"]";
}else{
str+="word"+array[i]+"#";
}
}
return str;
}
}
2. キーボードで文字列を入力し、その中に含まれるさまざまな文字の出現数を数えます。
package cn.hei.string;
import java.util.Scanner;
public class Work02StringCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String input = sc.next();
int countUpper = 0;
int countLower = 0;
int countNumber = 0;
int countOther = 0;
char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char ch = charArray[i];
if ('A' <= ch && ch <= 'Z'){
countUpper++;
}else if('a' <= ch && ch <= 'z'){
countLower++;
}else if ('0' <= ch && ch <='9'){
countNumber++;
}else {
countOther++;
}
System.out.println("大写字母有:"+countUpper);
System.out.println("小写字母有:"+countLower);
System.out.println("数字有:"+countNumber);
System.out.println("其他字符有:"+countOther);
}
}
}