有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不接受whats up之类的词

我试着让我的程序接受诸如whats up,hows things之类的词,但它并不是所有的连接都是诸如hello,hi之类的词,但不接受任何带有空格的词,如{whats up}有人能解释一下我做错了什么吗?我的代码没有错误,只是我试图让它接受像whats up,How you这样的词

    * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

    package bot;

    import java.util.*;

    /**
     *
     * @autr kesha
     */
    public class tst {
        public static void main(String[] args){
            Scanner input=new Scanner(System.in);



        HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
        // the ArrayList replaces your HashSet.
        ArrayList<String> list = new ArrayList<String>();   
        ArrayList<String> list1 = new ArrayList<String>();  
        Random r = new Random();

        list.add("Hello");
        list.add("Hi");
        list.add("How's you whats up");
        list.add("Hey");

        list1.add("Yuh mother");

        map.put("hi", list);
        map.put("whats up", list);
        map.put("hello", list);

        map.put("hoe", list1);


        System.out.println("enter Hi");
        String str = input.next().toLowerCase().trim();

        while(map.containsKey(str)){

        if (map.containsKey(str)) {
            ArrayList<String> tmpList = map.get(str); 

            int randomNumber = r.nextInt(tmpList.size()); 

            System.out.println(tmpList.get(randomNumber)); 

 System.out.println("enter Hi");

        str = input.next().toLowerCase().trim();
        }
    }

    }

共 (2) 个答案

  1. # 1 楼答案

    我相信。next()将字符串剪切到空白处。 试试这个

    String str = input.nextLine().toLowerCase().trim();
    
  2. # 2 楼答案

    您正在调用^{},它“从该扫描程序中查找并返回下一个完整的令牌”

    您希望调用^{},这需要一整行输入