代码:
import java.util.Scanner;
public class practice{public static void main(String[] args) {//键盘录入一个字符串,并进行遍历;Scanner input = new Scanner(System.in);System.out.println("输入一个字符串:");String a = input.next();for(int i=0;i<a.length();i++) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}
}
代码结果:
自己写的代码:
import java.util.Scanner;
public class practice{public static void main(String[] args) {//键盘录入一个字符串,并进行遍历;Scanner input = new Scanner(System.in);System.out.println("输入一个字符串:");String a = input.next();print(a);printReserve(a);print(a);}public static void printReserve(String a){for(int i=a.length()-1;i>-1;i--) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}public static void print(String a){for(int i=0;i<a.length();i++) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}
}
代码运行结果:
想让字符串里面的每个字符都加五:
代码:
import java.util.Scanner;
public class practice{public static void main(String[] args) {//键盘录入一个字符串,并进行遍历;Scanner input = new Scanner(System.in);System.out.println("输入一个字符串:");String a = input.next();print(a);printReserve(a);print(a);}public static void printReserve(String a){for(int i=a.length()-1;i>-1;i--) {char c =( char)(a.charAt(i)+5);System.out.print(c+"\t");}System.out.println();}public static void print(String a){for(int i=0;i<a.length();i++) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}
}
代码结果:
自己写代码:
import java.util.Scanner;
public class practice{public static void main(String[] args) {//键盘录入一个字符串,并进行遍历;Scanner input = new Scanner(System.in);System.out.println("输入一个字符串:");String a = input.next();print(a);count(a);//print(a);}public static void count(String a){int da =0;int xiao =0;int shu =0;for(int i=a.length()-1;i>-1;i--) {char c = a.charAt(i);if(c>97&&c<122) {xiao++;}else if(c>65&&c<90){da++;}else{shu++;}//System.out.print(c+"\t");}System.out.println("大写字母"+da+","+"小写字母"+xiao+","+"数字"+shu);}public static void print(String a){for(int i=0;i<a.length();i++) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}
}
截图:
视频代码: