51,409
社区成员
发帖
与我相关
我的任务
分享import java.util.Scanner;
public class homework3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入一段字符:");
String words=sc.next();
System.out.println("输入要查找的字符串:");
String toBeFound=sc.next();
int[] index=new int[10]; //存放下标位置的数组index
int count=0; //index数组元素下标计数
if(toBeFound.length()==1){
for(int i=0;i<words.length();i++){ //如果输入的是字符
if(words.substring(i,i+1).equals(toBeFound)){
index[count]=i;
count++;
}
}
}else if(toBeFound.length()>1){
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){//26行错误
index[count]=i;
count++;
}
}
}
System.out.println("“"+toBeFound+"”"+"出现的位置是:");
for(int i=0;i<index.length;i++){
if(index[i]!=999999999){ //未赋新值的index中的元素消去
System.out.print(index[i]+" ");
}
}
}
}
import java.util.Scanner;
public class homework3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入一段字符:");
String words=sc.next();
System.out.println("输入要查找的字符串:");
String toBeFound=sc.next();
int[] index=new int[10]; //存放下标位置的数组index
int count=0; //index数组元素下标计数
if(toBeFound.length()==1){
for(int i=0;i<words.length();i++){ //如果输入的是字符
if (words.length() >= i +1 ){
if(words.substring(i,i+1).equals(toBeFound)){
index[count]=i;
count++;
}
}
}
}else if(toBeFound.length()>1){
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if (words.length() >= i+toBeFound.length() ){ //先判断 words是否有 i + toBeFound.length()这么长
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){
index[count]=i +1; //出现位置 从1开始
count++;
}
}
}
}
System.out.println("“"+toBeFound+"”"+"出现的位置是:");
for(int i=0;i<index.length;i++){
if(index[i]!=999999999){ //未赋新值的index中的元素消去
System.out.print(index[i]+" ");
}
}
}
}
import java.util.Scanner;
public class homework3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入一段字符:");
String words=sc.next();
System.out.println("输入要查找的字符串:");
String toBeFound=sc.next();
int[] index=new int[10]; //存放下标位置的数组index
int count=0; //index数组元素下标计数
if(toBeFound.length()==1){
for(int i=0;i<words.length();i++){ //如果输入的是字符
if (words.length() >= i +1 ){
if(words.substring(i,i+1).equals(toBeFound)){
index[count]=i;
count++;
}
}
}
}else if(toBeFound.length()>1){
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if (words.length() >= i+toBeFound.length() ){ //先判断 words是否有 i + toBeFound.length()这么长
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){
index[count]=i +1; //出现位置 从1开始
count++;
}
}
}
}
System.out.println("“"+toBeFound+"”"+"出现的位置是:");
for(int i=0;i<index.length;i++){
if(index[i]!=999999999){ //未赋新值的index中的元素消去
System.out.print(index[i]+" ");
}
}
}
}