练习一:
输入字符串aba,依次输出各个字符
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String number=sc.next();
System.out.println("输入的字符串是"+number);
for(int i=0;i<number.length();i++){
char c=number.charAt(i);
System.out.println(c);
}
}
}
代码实现
java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String number = sc.next();
System.out.println("输入的字符串是:" + number);
// 使用 StringBuilder 来反转字符串
StringBuilder sb = new StringBuilder(number);
String reversed = sb.reverse().toString();
System.out.println("反转后的字符串是:" + reversed);
}
}
输出结果
练习 三:统计字符串次数
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=0,count1=0,count2=0;
String number=sc.next();
System.out.println("输入的字符串是"+number);
for(int i=0;i<number.length();i++){
char c=number.charAt(i);
if(c>='a'&&c<='z'){
count++;
}else if(c>='A'&&c<='Z'){
count1++;
}else if(c>='0'&&c<='9'){
count2++;
}else{
System.out.println("出错了");
}
}
System.out.println("大写字母有"+count1);
System.out.println("小写字母有"+count);
System.out.println("数字有"+count2);
}
}
输出结果:
练习四:拼接字符串
代码实现:
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
int arr[]= {1,3,5};
String pingjie="[ ";
for(int i=0;i<arr.length-1;i++){
pingjie=pingjie+arr[i]+",";
}
pingjie=pingjie+arr[arr.length-1]+"]";
System.out.println(pingjie);
}
}
实现结果
错误代码
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
int arr[]= {1,3,5};
String pingjie="[ ";
for(int i=0;i<arr.length-1;i++){
pingjie=pingjie+arr[i]+",";
}
pingjie=pingjie+arr[arr.length]+"]";
System.out.println(pingjie);
}
}
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
int arr[]= {1,3,5};
String result=arrtoString(arr);
System.out.println(result);
}
public static String arrtoString(int[] arr){
if(arr==null){
return " ";
}else if(arr.length==0){
return "[ ]";
}else {
String pingjie="[ ";
for(int i=0;i<arr.length-1;i++){
pingjie=pingjie+arr[i]+",";
}
pingjie=pingjie+arr[arr.length-1]+"]";
return pingjie;
}
}
}
输出结果
练习五:金额转换
实现思路:
代码实现:
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
String[] arr1={"百","十","万","千","百","十","元"};
//1.输入字符串
Scanner sc=new Scanner(System.in);
int cush;
while(true){
cush=sc.nextInt();
if(cush>0&&cush<9999999){break;}
else{
System.out.println("输入金额无效");
}
}
//1.1创建一个表,将数字对应其索引
String result = ""; // 初始化结果字符串
while (true) {
int number = cush % 10; // 取末尾数字
String num = capital(number); // 转换为文字
result = num + result; // 将转换后的文字添加到结果的开头
cush = cush / 10; // 去掉末尾数字
if (cush == 0) {
break; // 如果数字已经转换完毕,跳出循环
}
}
//判断缺多少个零,在字符串前面加上
int count=7-result.length();
for(int i=0;i<count;i++){
result="零"+result;
}
//打印结果
for (int i = 0; i < arr1.length; i++) {
char c=result.charAt(i);
System.out.print(c);
System.out.print(arr1[i]);
}
}
public static String capital(int number) {
String[] arr = {"零", "壹", "贰", "弎", "四", "伍", "六", "七", "八", "九", "十"};
return arr[number];
}
}
输出结果:
错误提醒:
javaint count=7-result.length(); for(int i=0;i<count;i++){ result="零"+result; }与 for(int i=0;i<7-result.length();i++){ result="零"+result; }输出零的结果为什么不一样
i<7-result.length()随result.length字符串的增长,i的范围逐渐减小,故输出的零的个数不对
练习六:转换罗马数字
代码实现
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
int num;
//1.输入字符串
Scanner sc = new Scanner(System.in);
String sentence;
while(true){
sentence = sc.next();
boolean flag=string(sentence);
if(flag==true){
System.out.println("输入正确");
break;
}else{
System.out.println("输入错误,请重新输入");
continue;
}
}
//2.将内容转变为字符串
for(int i=0;i<sentence.length();i++){
char c=sentence.charAt(i);
num=c-48;//ASCII
String result=turninto(num);
System.out.print(result+" ");
}
}
//判断输入的字符串是否合规
public static boolean string(String sentence) {
//1.长度《=9
if (sentence.length() > 9 ) {
return false;
}
for (int i = 0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
//2.只能是数字
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
public static String turninto(int num){
String[] arr={" ","I","II","III","Iv","V","VI","VII","VIII","IX"};
return arr[num];
}
}
易错点
- 长度判断 :条件应为
sentence.length() > 9 || sentence.length() <= 0
。原来的sentence.length() < 0
永远不会成立,因为length()
返回的是非负整数。
法二
java
import java.util.Scanner;
public class StringTomrs {
public static void main(String[] args) {
int num;
//1.输入字符串
Scanner sc = new Scanner(System.in);
String sentence;
while(true){
sentence = sc.next();
boolean flag=string(sentence);
if(flag==true){
System.out.println("输入正确");
break;
}else{
System.out.println("输入错误,请重新输入");
continue;
}
}
//2.将内容转变为字符串
for(int i=0;i<sentence.length();i++){
char c=sentence.charAt(i);
String result=turninto(c);
System.out.print(result+" ");
}
}
//判断输入的字符串是否合规
public static boolean string(String sentence) {
//1.长度《=9
if (sentence.length() > 9 ) {
return false;
}
for (int i = 0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
//2.只能是数字
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
public static String turninto(char num){
String str;
switch(num){
case'0'->str=" ";
case'1'->str="I";
case'2'->str="II";
case'3'->str="III";
case'4'->str="VI";
case'5'->str="V";
case'6'->str="VI";
case'7'->str="vII";
case'8'->str="VX";
default->str=" ";
}
return str;
}
}
练习七:
输入任意字符串,打乱其顺序
java
import java.util.Random;
import java.util.Scanner;
public class StringTomrs {
//输入任意字符串,打乱其顺序
public static void main(String[] args) {
String str="1234546";
char[]arr=new char[str.length()];
//打乱顺序
//1.使用subString
//2.将其转换为字符数组
arr=turnchar(str,arr);
//3.打乱顺序
arr=mix(arr);
//4.将字符数组转化为字符串
String string=new String(arr);
System.out.println(string);
}
//2.将其转换为字符数组
public static char[] turnchar(String str,char[]arr){
for (int i = 0; i < arr.length; i++) {
arr[i]=str.charAt(i);
}
return arr;
}
//3.打乱顺序
public static char[] mix(char[]arr){
Random rand=new Random();
char temp;
for (int i = 0; i < arr.length-1; i++) {
int suijizhi=rand.nextInt(arr.length);
temp=arr[i];
arr[i]=arr[suijizhi];
arr[suijizhi]=temp;
}
return arr;
}
}
法二
java
import java.util.Random;
public class StringTomrs {
public static void main(String[] args) {
String str = "1234546";
// 将字符串转换为字符数组
char[] arr = str.toCharArray();
// 打乱顺序
arr = mix(arr);
// 将字符数组转换为字符串
String shuffledString = new String(arr);
System.out.println(shuffledString);
}
// 打乱顺序
public static char[] mix(char[] arr) {
Random rand = new Random();
for (int i = arr.length - 1; i > 0; i--) {
int index = rand.nextInt(i + 1); // 生成 0 到 i 之间的随机数
// 交换 arr[i] 和 arr[index]
char temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
return arr;
}
}
输出结果哦
练习八:生成验证码
代码实现:
java
import java.util.Random;
public class StringTomrs {
public static void main(String[] args) {
Random rand = new Random();
//1.将所有大写小写字符都存放在一个数组中
char[] arr = new char[52];
arr = capital(arr);
//2.随机获取4个字符并拼接成zifuchuan
String str = "";
for (int i = 0; i < 4; i++) {
int num = rand.nextInt(arr.length);
str += arr[num];
}
// 2.将数字拼接在字符串后面并输出
int num = rand.nextInt(10);
str += num;
System.out.println(str);
//3.将其转换为字符数组
char[]arr1=new char[str.length()];
for(int i=0;i<str.length();i++){
arr1[i]=str.charAt(i);
}
// 4.打乱数数组的位置
//1.subString//2.for循环//3.
Random ran = new Random();
for (int i = arr1.length - 1; i > 0; i--) {
int j = rand.nextInt(i + 1); // 生成 [0, i] 之间的随机数
// 交换 arr[i] 和 arr[j]
char temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
String str1=new String(arr1);
System.out.println(str1);
}
//1.将所有大写小写字符都存放在一个数组中
public static char[] capital(char[]arr){
int index=0;
for(char i='a';i<'z';i++){
arr[index++]=i;
}
for(char i='A';i<'Z';i++){
arr[index++]=i;
}
return arr;
}
}
代码优化
java
import java.util.Random;
public class StringTomrs {
public static void main(String[] args) {
Random rand = new Random();
// 1. 将所有大写小写字符都存放在一个数组中
char[] arr = new char[52];
arr = capital(arr);
// 2. 随机获取4个字符并拼接一个数字
StringBuilder str = new StringBuilder();
for (int i = 0; i < 4; i++) {
int num = rand.nextInt(arr.length);
str.append(arr[num]);
}
int num = rand.nextInt(10);
str.append(num);
System.out.println("随机获取的4个字符和一个数字:" + str.toString());
// 3. 随机访问5个字符
StringBuilder str1 = new StringBuilder();
for (int i = 0; i < 5; i++) {
int index = rand.nextInt(str.length());
str1.append(str.charAt(index));
}
System.out.println("随机访问的5个字符:" + str1.toString());
}
// 将所有大写小写字符都存放在一个数组中
public static char[] capital(char[] arr) {
int index = 0;
for (char i = 'a'; i <= 'z'; i++) {
arr[index++] = i;
}
for (char i = 'A'; i <= 'Z'; i++) {
arr[index++] = i;
}
return arr;
}
}
修改说明:
大写字母的循环:在 capital 方法中,修正了大写字母部分的循环条件,确保包含 'z' 和 'Z'。
StringBuilder 的使用:使用 StringBuilder 替代字符串连接操作,以提升效率和性能。
随机访问的优化:在随机访问5个字符的部分,使用 StringBuilder 和随机索引来避免重复选择,确保5个字符顺序不同。
这样的修改可以提高代码的效率和可读性,并解决了原始代码中可能会出现的问题。
输出结果:
练习九:将字符串转化为数组
java
public class Solution {
//将字符串转化为整数
public static void main(String[] args) {
int sum=0;
char[] arr = new char[9];
String str=new String("123456789");
for(int i=0;i<str.length();i++){
arr[i]=str.charAt(i);
}
int[]arr1=new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arr1[i]=(int)(arr[i]-48);
}
for (int i = 0; i < arr1.length-1; i++) {
for(int j=0;j<arr1.length-1-i;j++){
arr1[j]=arr1[j]*10;
}
}
for (int i = 0; i < arr1.length; i++) {
sum+=arr1[i];
}
System.out.println(sum);
}
}
输出结果