开发者社区> 问答> 正文

使用扫描仪时找不到句子中的单词

我刚刚开始学习如何编写程序。在这个程序中。我正在尝试使用扫描仪在句子中查找单词。但是,我遇到了一个问题。我的代码如下:

import java.util.Scanner;

public class TestOne {
    static Scanner scn = new Scanner(System.in);

    public static void main(String[] args) {
        if (pick == 5) {
            String sentenceFive = "i see you";
            String wordFive = "you";

            if (sentenceFive.contains(wordFive)) {
                System.out.println("Keyword matched the string");
            }

            else {
                System.out.println("No match");
            }
        }  else if (pick == 6) {
            System.out.println("Please enter a sentence");
            String sentenceFive = scn.nextLine();
            scn.nextLine();
            System.out.println("Please enter a word");
            String wordFive = scn.nextLine();

            if (sentenceFive.contains(wordFive)) {
                System.out.println("Keyword matched the string");
            }

            else {
                System.out.println("No match");
            }

        }
    }

}

(pick==5)工作完全正常,但(pick==6)返回“不匹配”。在if与else语句都是相同的,我的投入(pick==6)也是相同的字符串(pick==5)。所以我想这是因为扫描仪吧?

展开
收起
几许相思几点泪 2019-12-05 15:31:10 271 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载