paizaラーニング Bランクレベルアップメニュー 占い Java 解答

# STEP: 57 連想配列（辞書）を使う
## 問題
https://paiza.jp/works/mondai/prob60/fortune_telling_0

## 解答
```java:step57java

public class Main {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("Kyoko", "B");	
        map.put("Rio", "O");	
        map.put("Tsubame", "AB");	
        map.put("KurodaSensei", "A");
        map.put("NekoSensei", "A");
        

        System.out.println("Kyoko" + " " + map.get("Kyoko"));
        System.out.println("Rio" + " " + map.get("Rio"));
        System.out.println("Tsubame" + " " + map.get("Tsubame"));
        System.out.println("KurodaSensei" + " " + map.get("KurodaSensei"));
        System.out.println("NekoSensei" + " " + map.get("NekoSensei"));      
    }
}
```
ださい・・・
LinkedHashMapを次から使おう。。。

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/72fbf9bc-8020-9731-0268-f0bcc076b7e7.png)

# STEP: 58 ユーザーの血液型のデータ処理
## 問題
https://paiza.jp/works/mondai/prob60/fortune_telling_1
## 解答
```java:step58.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        // 行を送る
        sc.nextLine();
        LinkedHashMap<String, String> map = createAssociativeArray(n, sc);
        for (Map.Entry<String, String> entry : map.entrySet()){
            String user = entry.getKey();
            String blood = entry.getValue();
            System.out.println(user + " " + blood);
        }
        sc.close();
    }
    
    /**
     * userとbloodの辞書配列を作成する関数
     * 
     *  @param sc userとbloodの標準入力
     *  @param n 行数
     * 
     *  @return 辞書配列
     * */
    public static LinkedHashMap createAssociativeArray(int n, Scanner sc){
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        
        for(int i = 0; i < n; i++){
            String user = sc.next();
            String blood =  sc.next();
            map.put(user, blood);    
        }
        
        return map;
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/04fd5aae-af7a-cff4-01b0-ae07cbb51523.png)

# STEP: 59 1人の血液型
## 問題
https://paiza.jp/works/mondai/prob60/fortune_telling_2
## 解答
```java:step59.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String user = sc.nextLine();
        int n = sc.nextInt();
        // 改行
        sc.nextLine();
        Map<String, String> map = createMap(n, sc);
        System.out.println(user + " " +  map.get(user));
        
    }
    
    /**
     * userとbloodの辞書配列を作成
     * 
     * @param n 行数
     * @param sc  userとbloodの標準入力
     * 
     * @return 辞書配列
     **/
    public static Map createMap(int n, Scanner sc){
        Map<String, String> map = new HashMap<String, String>();
        for(int i = 0; i < n; i++){
            map.put(sc.next(), sc.next());
        }
        return map;
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/5bfc5776-1488-bb1b-9b4a-d5e2dc2a0dc7.png)

# STEP: 60 1つの血液型を占う
## 問題
https://paiza.jp/works/mondai/prob60/fortune_telling_3

## 解答
```java:step60.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String blood = sc.nextLine();
        int n = sc.nextInt();
        // 改行
        sc.nextLine();
        Map<String,String> map = createMap(n,  sc);
        System.out.println(map.get(blood));
    }
    
    /**
     * 血液と占い結果のMapを作成
     * 
     * @param n 行数
     * @param 血液と占い結果の標準入力
     * 
     * @return  血液と占い結果のMap
     * */
    public static Map<String, String> createMap(int n, Scanner sc){
        Map<String, String> map = new HashMap<String, String>();
        for(int i = 0; i < n; i++){
            map.put(sc.next(), sc.next());
        }
        return map;
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/f32be11f-4e2e-d5f3-e916-a22112074e4d.png)

# STEP: 61 1人の占い結果
## 問題
https://paiza.jp/works/mondai/prob60/fortune_telling_4

## 解答
```java:step61.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int n = sc.nextInt();
        
        // 改行
        sc.nextLine();
        
        // ユーザー
        Map<String, String> user = createMap(n, sc);

        // 改行
        sc.nextLine();
        
        n = sc.nextInt();
        
        // 改行
        sc.nextLine();       
        
        // 占い
        Map<String, String> divination = createMap(n, sc);
        
        String value = user.get(name);
        System.out.println(divination.get(value));
    }
    
    /**
     * mapの作成
     * 
     * @param n 行数
     * @param 標準入力
     * 
     * @return map
     * */
    public static Map createMap(int n, Scanner sc){

        Map<String, String> map = new HashMap<String, String>();
        for(int i = 0; i < n; i++){
            map.put(sc.next(), sc.next());
        }

        return map;
    }

}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/9e69358f-da60-79f5-182f-be646644ec73.png)

# FINAL問題 占い
## 問題
https://paiza.jp/works/mondai/prob60/fortune_telling_9

## 解答
```java:final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        // 改行
        sc.nextLine();
        LinkedHashMap<String, String> user = createMap(n, sc);
        // 改行
        sc.nextLine();
        n = sc.nextInt();
        // 改行
        sc.nextLine();
        LinkedHashMap<String, String> divination = createMap(n, sc);
        
        for(Map.Entry<String, String> entry : user.entrySet()){
             System.out.println(entry.getKey() + " " + divination.get(entry.getValue()));
        }
        
        sc.close();
       
    }
    
    /**
     * Map作成する関数
     * 
     * @param 行数
     * @param 標準入力
     * 
     * @return map
     */
    public static LinkedHashMap<String, String> createMap(int n, Scanner sc){
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        for(int i = 0; i < n; i++){
            map.put(sc.next(), sc.next());
        }
        return map;
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/4226c52a-0090-1c80-5a8e-4f3d248364e5.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/a465f93f-cabb-d1a2-7922-777f47aa2c9b.png)