在一次采访中我被问到一个问题:
从控制台获取输入,例如:“欢迎来到世界”并计算用户输入的特定字符,在不使用任何内置方法(如{{1})的情况下,索引和次数(即字符的出现次数)等等。
答案 0 :(得分:7)
给你一个盆栽的答案根本无济于事。以下是您如何实现自己想要的目标。尝试自己编写代码。
答案 1 :(得分:0)
import java.io.Console;
class dev
{
public static void main(String arg[])
{
Console c=System.console();
String str=c.readLine();
System.out.println(str);
int times=0;
//find c character
char find='c';
char strChar[]=str.toCharArray();
System.out.print("positions of char c in the string :");
try
{
for (int i=0; ;i++ )
{
if(strChar[i]==find)
{
System.out.print((i-1)+", ");times++;
}
}
}
catch (Exception e)
{
System.out.println("\n"+"The no. of times c occur : "+times);
}
}
}