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

TOP

返回列表