1234567891011121314151617181920212223242526 |
- package J20250715.homework;
- import java.util.Scanner;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo01
- * @description 遍历字符串案例
- * @create 2025/7/15
- */
- public class Demo01 {
- public static void main(String[] args) {
- System.out.println("请输入一段字符串....");
- //创建Scanner对象
- Scanner sc=new Scanner(System.in);
- //获取字符串的长度的方法:字符串变量.length();
- String s = sc.nextLine();
- int length=s.length(); //获取s字符串的长度,赋值给一个整型变量
- for (int i = 0; i < length; i++) {
- char c=s.charAt(i);
- System.out.println(c);
- }
- }
- }
|