62,620
社区成员
发帖
与我相关
我的任务
分享
String[] shu= new String[wenzi.length()];
for (int i = 0; i < shu.length; i++) {
shu[i]=wenzi.substring(i,i+1);
}
import java.util.Scanner;
public class B {
/**
* @param args
*/
public static void main(String[] args) {
String wenzi = "0";
char word;
Scanner input = new Scanner(System.in);
System.out.println("请输入一段文字:");
wenzi = input.next();
char[] shu = new char[wenzi.length()];//注意在输入了字符串之后在动态分配数组长度
System.out.println("请输入要查询的字符:");
word = input.next().charAt(0);
System.out.print(word + "出现的位置是:");
for (int i = 0; i < wenzi.length(); i++) {
shu[i] = wenzi.charAt(i);
}
for (int i = 0; i < shu.length; i++) {
if (shu[i] == word) {
System.out.println((i + 1) + "\t");
}
}
}
}
import java.util.Scanner;
public class StringExplorer
{
/**
* @param args
*/
public static void main(String[] args)
{
String wenzi="0";
String word;
String[] shu;
Scanner input = new Scanner(System.in);
System.out.println("请输入一段文字:");
wenzi = input.next();
System.out.println("请输入要查询的字符:");
word = input.next();
shu= new String[wenzi.length()-(word.length()-1)];//注意这个长度.
System.out.print(word+"出现的位置是:\n");
for (int i = 0; i < shu.length; i++)
{
shu[i]=wenzi.substring(i,i+word.length());//注意取得的子串长
}
for (int i = 0; i < shu.length; i++)
{
if (shu[i].equals(word))
{
System.out.println((i+1)+"\t");
}
}
}
}