一个集成输入查找功能的的java简单程序

这个Java程序展示了如何从键盘读取输入,并在读取的文本中查找特定字符。`ReadFromKeyboard`类负责捕获键盘输入,而`Sch`类用于查找并计数指定字符出现的次数。在`ReadTest3`主程序中,计算了整个操作的运行时间。
摘要由CSDN通过智能技术生成

//该类捕抓键盘输入
import java.io.*;
public class ReadFromKeyboard{
    public String text=null;
    public String ReadMothod(){
   InputStreamReader converter = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(converter);
   
    try{
       text= in.readLine();
      
      
     
     }catch(IOException e){System.out.println(e);}
      return text;
    
 
  }
}
//Sch类作为查找词语被调用
class Sch{
    Sch(){}
    Sch(String str,char ch){
        int count=0;
     
            System.out.print("/""+ch+"/""+"出现的位置分别为:");
       
      
        for(int i=0;i!=-1;){     //根据indesOf()方法,如果找到就返回第一次找到的位置,如果找不到就返回-1
            int j=0;
   j= str.indexOf(ch, i + j);
   i=j;
            if(i!=-1)
              {
                count++;
                System.out.print(i+" ");
                i++;
                }
             }
        
        if(count>0)
       
            System.out.println("/n"+ch+"共出现了"+count+"次");
       
        else
            System.out.println("没有在您输入的字符串中找到字符"+ch);
        }
     }

//主程序
public class ReadTest3 {

    public static void main(String[] args) {
     long t1=System.currentTimeMillis(),t2;
        ReadTest2 s1=new ReadTest2();
        String str=s1.ReadMothod();
    //    char str2=str.charAt(0);
    //    System.out.println(str2);
        Sch s2=new Sch(str,'a');
    
        t2=System.currentTimeMillis();
        System.out.println("程序运行时间为:"+(t2-t1)/1000+"豪秒.");
     
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值