【问题标题】:How to scan lines inside a loop and insert it in ArrayList? [duplicate]如何扫描循环内的行并将其插入 ArrayList? [复制]
【发布时间】:2019-11-06 21:25:29
【问题描述】:

我正在创建一个包含用户在控制台中输入的字符串的数组列表。问题是当他们输入超过 2 个单词的 String 时,程序无法按预期运行。

这是我目前所拥有的:

  ArrayList<String> list = new ArrayList<String>();      
  Scanner scan = new Scanner (System.in);      
  int i;

  System.out.print("How many TV shows do you hope to watch this week? ");
  i = scan.nextInt(); 
  scan.nextLine();

  for(int j = 0; j<i; j++){
     System.out.print("Enter show " + (j+1) + ": ");
     list.add(scan.nextLine());
  }

  System.out.print("Have you caught up to any shows (answer yes or no): ");

   while (scan.nextLine().equalsIgnoreCase("yes")){
     System.out.print("Which show? ");
     String show = new String(scan.nextLine());
     if(list.contains(show)){
        list.remove(list.indexOf(show));
     }else { 
        System.out.print("That show is not on original list!");
     }
  }


  System.out.println("Here's what you still have to watch this week:");
  System.out.println(list);        

顺便说一句,我已经尝试从 next() 更改为 nextLine() 它仍然无法按预期工作。

我的预期输出如下:

示例 1:假设您没有赶上任何节目 (这工作,感谢用户的回应,在下面)

 How many TV shows do you hope to watch this week? 3
 Enter show 1: RWBY
 Enter show 2: Kengan Ashura
 Enter show 3: The Good Place
 Have you caught up to any shows (answer yes or no): no
 Here's what you still have to watch this week:
 [RWBY, Kengan Ashura, The Good Place]

示例 2:假设您已经赶上了一个节目并将更新列表 (不工作)

 How many TV shows do you hope to watch this week? 3
 Enter show 1: Father Brown
 Enter show 2: Death in Paradise
 Enter show 3: Watchmen
 Have you caught up to any shows (answer yes or no): yes
 Which show? Death in Paradise
 Any other shows you're caught up with? (yes/no) no
 Here's what you still have to watch this week:
 [Father Brown, Watchmen]

示例 3:假设您赶上了一个节目,但它不在列表中。 (它不会更新列表,因为将您赶上的新节目添加到列表中只是为了删除它是没有意义的。)

 How many TV shows do you hope to watch this week? 2
 Enter show 1: Watchmen
 Enter show 2: RWBY
 Have you caught up to any shows (answer yes or no): yes
 Which show? Monday Night Football
 That show is not on original list!
 Any other shows you're caught up with? (yes/no) no
 Here's what you still have to watch this week:
 [Watchmen, RWBY]

谢谢!

【问题讨论】:

  • 不,如果你测试我的代码,你会注意到如果你从 next() 更改为 nextLine() 它仍然不能按预期工作。
  • 还有stackoverflow.com/questions/13102045/…,需要看nextInt()后面的返回行
  • 这也没有帮助。 :(
  • @MarkSantos 你能发布你的实际输出吗?
  • 我试过了,它有效,用这段代码检查你使用 scnanner 的方式与我在 cmets ideone.com/1XzEHz987654322@ 中指出的两个地方相同

标签: java string for-loop java.util.scanner


【解决方案1】:

我是这样得到你可能需要的东西的:

    Set<String> list = new HashSet<>();
    Scanner scan = new Scanner (System.in);

    System.out.print("How many TV shows do you hope to watch this week? ");
    int i = Integer.parseInt(scan.nextLine());

    for(int j = 0; j<i; j++){
        System.out.print("Enter show " + (j+1) + ": ");

        list.add(scan.nextLine());
    }

    System.out.print("Have you caught up to any shows (answer yes or no): ");

    String showName;
    String answer;
    do {
        answer = scan.nextLine();
        if (answer.equalsIgnoreCase("no")) {
            break;
        } else {
            System.out.print("Which show? ");

            showName = scan.nextLine();
            if(list.contains(showName)){
                list.remove(showName);
            } else {
                System.out.println("That show is not on original list!");
            }

            System.out.println("Any other shows you're caught up with? (yes/no): "); // What yo do if I don't 
            // enter a "yes" or a "no"?
        }
    } while(answer.equalsIgnoreCase("yes"));

    System.out.println("Here's what you still have to watch this week:");
    System.out.println(list);
  1. 首先,我建议您使用Set&lt;String&gt; 而不是List&lt;String&gt;,就像我在代码中所做的那样。
  2. 其次,想想用户可以在哪些地方输入不应该输入的值,以及如何处理它们。比如,当你想输入观看的节目数量时,如果我输入一个单词,就会抛出异常,如何处理?如果我输入“是”或“否”以外的任何内容,如何处理这种情况?

【讨论】:

    【解决方案2】:

    我没有发现用户输入多个字符串的问题。 这是我所拥有的:

              ArrayList<String> list = new ArrayList<String>();      
              Scanner scan = new Scanner (System.in);      
              int i; String in;
    
              System.out.print("How many TV shows do you hope to watch this week? ");
              i = scan.nextInt(); 
              scan.nextLine();
    
              for(int j = 0; j<i; j++){
                 System.out.print("Enter show " + (j+1) + ": ");
                 list.add(scan.nextLine());
              }
    
    
              System.out.print("Have you caught up to any shows (answer yes or no): ");
              in = scan.nextLine(); 
    
              if(in.equalsIgnoreCase("yes")) {
                  boolean more = true;
    
                  while(more) {
                      System.out.print("Which show? ");
                      String show = new String(scan.nextLine());
                      if(list.contains(show)){
                         list.remove(list.indexOf(show));
                      }else { 
                         System.out.println("That show is not on original list!");
                      }
                      System.out.print("Any other shows you're caught up with? (yes/no): ");
                      in = scan.nextLine(); 
                      if(!in.equalsIgnoreCase("yes")) {
                          more = false;
                      }
                  }
    
              }
    
              System.out.println("Here's what you still have to watch this week:");
              System.out.println(list);
    

    我使用了一个 while 循环来检测用户是否有更多他们关注的节目。

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      相关资源
      最近更新 更多