检查扫描仪输入是否与数组中的字符串匹配

时间:2021-01-31 18:16:54

标签: arraylist java.util.scanner capitalization

我正在编写一个程序,询问用户他们的名字以及他们的感受。如果为“name”输入,我想让程序首先大写姓氏的第一个字母。其次,我希望程序将扫描仪输入与数组列表中包含的字符串进行比较。目前,如果用户输入了多个单词,则“else”输出将打印两次或更多次。如何让程序接受多个单词作为输入,然后将其与数组列表内容进行比较?我尝试过使用缓冲区读取器和分隔扫描器,但没有成功。我的代码如下:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Conversation {
  public static void main(String[] args) {
     List<List<String>>goodfeeling = new ArrayList<>();
 
     goodfeeling.add(Arrays.asList("never better", "okay", "alright", "not bad", "couldn't be better"));
     List<List<String>> badfeeling = new ArrayList<>();
     badfeeling.add(Arrays.asList("not good", "not good at all", "could be better", "bored"));
     Scanner userinput = new Scanner(System.in); 
     String response = "", name = "";
     Integer g = 0;
     System.out.println("Welcome to the Conversation app");

     System.out.println("*************************************************************************"); 
     System.out.println("Hello there! May I ask your name?");
     name += userinput.nextLine();
     **String upperCase = name.substring(0, 1).toUpperCase() + name.substring(1);**
     //Only Capitalizes first letter of first name, but not the first letter of the last name.
     System.out.println("Thank you " + upperCase + ", for providing your name.");

     System.out.println("*************************************************************************");
     System.out.println("So " + upperCase + ", how are you feeling today?");
     response = userinput.nextLine();
   
        while (true) {
           if (goodfeeling.get(g).contains(response)) {
              System.out.println("Nice! I'm happy to hear that!");
           }else if (badfeeling.get(g).contains(response)) {             
              System.out.println("Aww. I'm sorry to hear that.");                  
           }else {**strong text** 
              System.out.println("Sorry, I didn't quite get that, please try again?");
              response = userinput.nextLine();
              continue;
          }
          
      } 
 //If 'not too bad' is entered, for example, then 'Sorry, I didn't quite get that' is printed three times.
 }

0 个答案:

没有答案