返回列表 發帖

TQC210 - 字元搜尋器

本帖最後由 tonyh 於 2013-7-27 17:29 編輯
  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.                /* boolean 變數的值只有兩種 true 或 false
  14.                   find 變數用來控制 "第幾個位置找到了" 的出現
  15.                   find1 變數用來控制 "搜尋的字元不在字串中" 的出現 */
  16.         do
  17.         {
  18.             loc=str.indexOf(str1,loc+1);
  19.             if(loc>-1)
  20.             {
  21.                 if(!find)   //等同於 find=false
  22.                 {
  23.                      System.out.println("第幾個位置找到了");
  24.                      find=true;
  25.                 }
  26.                 find1=true;
  27.                 System.out.println(loc+1);
  28.             }
  29.         }while(loc!=-1);
  30.         if(!find1)
  31.             System.out.println("搜尋的字元不在字串中");
  32.     }
  33. }
複製代碼

  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

  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(str,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(!find1)
  28.             System.out.println("搜尋的字元不在字串中");
  29.      }
  30. }
複製代碼

TOP

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



  28.   }
  29. }
複製代碼

TOP

  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

返回列表