返回列表 發帖
  1. import java.util.Scanner;
  2. public class TQC210
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 boolean find,indexfind;
  7.                 System.out.println("請輸入字串");
  8.                 Scanner s=new Scanner(System.in);
  9.                 String str=s.nextLine();
  10.                 System.out.println("請輸入要搜尋的字元或字串");
  11.                 String str2=s.nextLine();
  12.                 int index=-1;
  13.                 find=false;
  14.                 indexfind=false;
  15.                 do
  16.                 {
  17.                         if((index=str.indexOf(str2,index+1))>-1)
  18.                         {
  19.                                 find=true;
  20.                                 if(!indexfind)
  21.                                 {
  22.                                         System.out.println("第幾個位置找到了");
  23.                                         indexfind=true;
  24.                                 }
  25.                                 System.out.println(index+1);
  26.                         }
  27.                 }while(index!=-1);
  28.                 if(!find)
  29.                 System.out.println("搜尋字元不在字串中");
  30.         }
  31. }
複製代碼
小雲雀

TOP

返回列表