题目1:键盘录入一个字符串,统计字符串大小写字母和数字字符的个数。
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args){
//1.从键盘动态输入一个字符串
System.out.println("请输入一个字符串(包含大小字母及数字字符):");
Scanner input = new Scanner(System.in);
String strInput = input.nextLine();
//2.统计大小写字母及数字字符的个数
int countBig = 0;
int countSmall = 0;
int countNum = 0;
for(int i = 0; i < strInput.length(); i ++){
if(strInput.charAt(i) >= 'a' && strInput.charAt(i) <= 'z'){
countSmall ++;
}else if(strInput.charAt(i) >= 'A' && strInput.charAt(i) <= 'Z'){
countBig ++;
}else if(strInput.charAt(i) >= '0' && strInput.charAt(i) <= '9'){
countNum ++;
}
}
//3.输出结果
System.out.println("这个字符串中大写字母的个数有" + countBig + "个;");
System.out.println("这个字符串中大写字母的个数有" + countSmall + "个;");
System.out.println("这个字符串中大写字母的个数有" + countNum + "个。");
}
}
题目2:定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。例如:Hello1Hello2Hello3
public class Demo {
public static void main(String[] args){
//1.定义一个整型数组
int[] array = new int[]{1,2,3};
//2.调用方法将数组转换为字符串
String strRestructuring = arrayToString(array);
//4.打印输出结果
System.out.println("这个是拼接的字符串:" + strRestructuring);
}
//3.定义一个将整型数组转换为字符串数组的方法
public static String arrayToString(int[] array){
//定义一个字符串对象
String strRestructuring = new String();
//遍历数组,将每一个数转换成相对应的字符
for(int i = 0; i < array.length; i ++){
//调用String拼接方法
strRestructuring = strRestructuring.concat("Hello" + array[i]);
}
return strRestructuring;
}
}