Friend Info Query


Submit solution

Points: 50 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type

題目說明

請補齊主程式下半部分,讓程式能根據使用者的輸入,查詢指定朋友的指定資訊(如:age 或 city),並印出對應的結果。

✅ 注意:請勿更改上方建立 friends 資料的程式碼,僅修改下方查詢與輸出邏輯。

程式模板

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class MyProgram {
    public static void main(String[] args) {
        ArrayList<HashMap<String, String>> friends = new ArrayList<>();

        HashMap<String, String> person1 = new HashMap<>();
        person1.put("name", "John");
        person1.put("age", "30");
        person1.put("city", "New York");
        friends.add(person1);

        HashMap<String, String> person2 = new HashMap<>();
        person2.put("name", "Alice");
        person2.put("age", "25");
        person2.put("city", "Los Angeles");
        friends.add(person2);

        // --- TODO: 補齊主程式 ---

    }
}

輸入

輸入共兩行:

  • 第一行為查詢對象的 name(如:John)。

  • 第二行為查詢的欄位名稱(如:city 或 age)。

輸出

若找到該名朋友,請印出其對應的欄位內容。

格式:<查詢對象的名字>'s <欄位名稱> is <對應資料>(請依據輸入動態變化)。

若找不到該名朋友,請輸出:Friend not found

若欄位不存在,請輸出:Invalid field

範例輸入 #1

John
city

範例輸出 #1

John's city is New York

範例輸入 #2

Bob
city

範例輸出 #2

Friend not found

範例輸入 #3

John
email

範例輸出 #3

Invalid field

Comments

There are no comments at the moment.