String d1="I like to watch the sun set with my friend.";
String d2="The Best Places To Watch The Sunset.";
String d3="My friend watch the sun come up.";
句子是d1 d2 d3,输入句子中的单词,返回单词所在句子编号

解决方案 »

  1.   

    d1.contains("friend");
      

  2.   


    public static void main(String[] args) {
    String d1="I like to watch the sun set with my friend.";
    String d2="The Best Places To Watch The Sunset.";
    String d3="My friend watch the sun come up.";

    String word = "My";
    String resultString = "单词在";
    if(d1.contains(word)){
    resultString += "d1、";
    }
    if(d2.contains(word)){
    resultString += "d2、";
    }
    if(d3.contains(word)){
    resultString += "d3、";
    }
    resultString = resultString.substring(0,resultString.length()-1);
    resultString += "中";
    System.out.println(resultString);
    }
      

  3.   

    String d1="I like to watch the sun set with my friend.";
    String d2="The Best Places To Watch The Sunset.";
    String d3="My friend watch the sun come up.";

    String demo = "like";
    if (d1.contains(demo)) {
    System.out.println("搜索结果在的d1中");
    }else if(d2.contains(demo)){
    System.out.println("搜索结果在的d2中");
    }else if(d3.contains(demo)){
    System.out.println("搜索结果在的d3中");
    }else{
    System.out.println("您的搜索不在范围内!");
    }
      

  4.   

    public class CheckExist {

    public static final String SPACE = " "; public static void main(String[] args) {
    String d1="I like to watch the sun set with my friend.";
    String d2="The Best Places To Watch The Sunset.";
    String d3="My friend watch the sun come up.";
    String[] array = {d1,d2,d3};
    System.out.println(confirm(array, "come"));
    }

    public static String confirm(String[] array,String word){
    for (String s : array) {
    String[] arr = s.split(SPACE);
    for (String string : arr) {
    if (string.equals(word)) {
    return s;
    }
    }
    }
    return "No Such Word!";
    }}