2018/12/07 L1-035 情人節 Java
阿新 • • 發佈:2018-12-08
題目是簡單的, 但是一直有一個點沒有通過, 最後我發現我沒有考慮如果直接輸入"." , 也就是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) throwsException{ 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..."); } } }