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块就可以了

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)