hideatbush 2017-04-25 16:13 采纳率: 0%
浏览 887

如何才能去掉这么多case ,使起逐次输出字符串中的元素,一定用switch。

public static void main(String[] args) throws InterruptedException {

    String str = "abcdefghijklmnop";
    int i = 1;
    switch (i) {
    case 1:
    System.out.println(str.charAt(0));
    case 2:
        System.out.println(str.charAt(1));
    case 3:
        System.out.println(str.charAt(2));  
    case 4:
        System.out.println(str.charAt(3));
    case 5:
        System.out.println(str.charAt(4));
    case 6:
        System.out.println(str.charAt(5));
    case 7:
        System.out.println(str.charAt(6));
    case 8:
        System.out.println(str.charAt(7));
    case 9:
        System.out.println(str.charAt(8));
    case 10:
        System.out.println(str.charAt(9));
    case 11:
        System.out.println(str.charAt(10)); 
    case 12:
        System.out.println(str.charAt(11));
    case 13:
        System.out.println(str.charAt(12));
    case 14:
        System.out.println(str.charAt(13));
    case 15:
        System.out.println(str.charAt(14));
    case 16:
        System.out.println(str.charAt(15));
    }   
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2017-04-25 16:14
    关注

    System.out.println(str.charAt(i - 1));

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python