返回列表 發帖
  1. import java.io.*;
  2. import java.util.Scanner;


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

TOP

返回列表