2018/12/07 L1-035 情人节 Java
题目是简单的, 但是一直有一个点没有通过, 最后我发现我没有考虑如果直接输入"." , 也就是LinkedList容器内没有元素时的情况
这道题目给我的启示是, 写简单的题目, 要万分小心, 把该考虑的情况都要考虑清楚了, 这才能把分数都拿到手.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Queue<String> linkedlist = new LinkedList<String>(); while(true) { String str = br.readLine(); if(str.equals(".")) { break; } else if(str.equals("")) { break; } else if (str.length() > 10) { break; } else { linkedlist.add(str); } } if (linkedlist.isEmpty()) { System.out.print("Momo... No one is for you ..."); return; } if (linkedlist.size() == 1) { System.out.print("Momo... No one is for you ..."); return; } else if (linkedlist.size() >= 2 && linkedlist.size() < 14) { linkedlist.remove(); String name = linkedlist.poll(); System.out.print(name + " is the only one for you..."); return; } else { linkedlist.remove(); String second_name = linkedlist.poll(); for(int i=0; i<11; i++) { linkedlist.remove(); } String fifteen_name = linkedlist.poll(); System.out.print(second_name+" and "+fifteen_name+" are inviting you to dinner..."); } } }