dl852718724 2017-09-27 08:16 采纳率: 0%
浏览 669

初学java,求大神解答

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条回答

  • T_world 2017-09-27 11:31
    关注

    String类型的变量有一个contains方法,用它来判断就好了,代码如下:

    package com.tt.test;
    
    import java.util.Scanner;
    
    public class Test{
        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.";
    
            Scanner scan = new Scanner(System.in);
            while(scan.hasNext()){
                String input = scan.nextLine();
                if(d1.contains(input)){
                    System.out.println(input+"被包含在"+d1+"这句话中");
                }else if (d2.contains(input)){
                    System.out.println(input+"被包含在"+d2+"这句话中");
                }else if(d3.contains(input)){
                    System.out.println(input+"被包含在"+d2+"这句话中");
                }else{
                    System.out.println("不存在的单词!");
                }
            }
        }
    } 
    

    我是把所有逻辑都写在main方法里了,如果需要抽取成单独的一个方法主要抽取if-else if-else块就可以了

    评论

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了