需求: 键盘录入一个字符串,使用程序实现在控制台遍历该字符串
package String;import java.util.Scanner;public class StringDemo5 {public static void main(String[] args) {//录入一个字符串Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String str = sc.next();//进行遍历for (int i = 0; i < str.length(); i++) {// i 依次表示字符串的每一个索引//自动生成左边是 Ctrl + alt + vchar c = str.charAt(i);System.out.println(c);}}
}
运行结果
统计字符次数
键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
package String;import java.util.Scanner;public class StringDemo6 {public static void main(String[] args) {//键盘录入Scanner sc = new Scanner(System.in);System.out.println("输入一个字符串");String str = sc.next();//统计 计数器思想//定义三个计数器int bigCount = 0;int smallCount = 0;int numberCount = 0;for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c>='a' && c<='z'){char类型的变量在参与计算的时候自动类型提升为int查询ascii码表smallCount++;}else if(c>='A' && c<='Z'){bigCount++;}else if(c>='0' && c<='9'){numberCount++;}}System.out.println("小写字母有:"+smallCount+"个");System.out.println("大写字母有:"+bigCount+"个");System.out.println("数字字母有:"+numberCount+"个");}
}
拼接字符串
package String;import com.sun.source.tree.IfTree;import javax.xml.transform.Result;public class StringDemo7 {public static void main(String[] args) {int[] arr = {1,2,3};String str = arrToString(arr);System.out.println(str);//[123}//定义一个方法public static String arrToString(int[] arr){if (arr == null){return "";}if (arr.length == 0){return "[]";}String result = "[";//代码到这表示数组不是null 也不是长度为0的for (int i = 0; i < arr.length; i++) {//i 索引 arr[i] 元素if (i == arr.length - 1){result = result + arr[i];}else {result = result + arr[i] + ", ";}}result = result + "]";return result;}
}
字符串反转
定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果例如,键盘录入 abc,输出结果 cba
package String;import javax.xml.transform.Result;public class StringDemo8 {public static void main(String[] args) {String result = reverser("abc");System.out.println(result);}public static String reverser(String str) {String result ="";for (int i = str.length() -1 ; i >= 0; i--) {//依次表示字符串中的每一个索引(倒着的)char c = str.charAt(i);result = result + c ;}return result;}
}
运行结果
金额转换
代码
package String;import java.util.Scanner;public class StringDemo9 {public static void main(String[] args) {//键盘录入一个金额Scanner sc = new Scanner(System.in);int money ;while (true) {System.out.println("输入一个金额");money = sc.nextInt();if (money >= 0 && money <= 99999999) {break;} else {System.out.println("输入金额无效");}}//定义一个变量用来表示钱的大写String moneyStr = "";//System.out.println(money);// 得到money里面的每一位数while (true){//从右往左获取数据int ge = money % 10;String capitalNumber = getCapitalNumber(ge);//把转换之后的大写拼接到moneystr中moneyStr = capitalNumber+moneyStr;money = money / 10;//如果数字上的每一位都获取到了,那么money记录的就是0,此时循环结束if (money == 0) {break;}}//在前面补0补齐七位int count = 7 - moneyStr.length();for (int i = 0; i < count; i++) {moneyStr="零"+ moneyStr;}//插入单位//定义一个数组表示单位String[] arr = {"佰","拾","萬","仟","佰","拾","元"};//遍历moneystr//然后把arr的单位插入进去String result = "";for (int i = 0; i < moneyStr.length(); i++) {char c = moneyStr.charAt(i);result = result + c + arr[i];}System.out.println(result);}//定义一个方法把数字变成大写的数字public static String getCapitalNumber(int number){//定义数组让数字跟大小的中文产生关系String[] arr ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};//返回结果return arr[number];}
}