筆試IO_template(java)

/**
 * Copyright (C), 2018-2020
 * FileName: IO_template
 * Author:   xjl
 * Date:     2020/6/7 21:09
 * Description: 輸入輸出的測試用例
 */
package IO_Template;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * 平時寫程序一般不用Scanner,線上筆試的時候,各大公司熱衷於Scanner輸入。
 * 平時用LeetCode刷題也不會用到,結果多次在筆試時候卡在Scanner,特來總結一波。
 */
public class IO_template {

    public static void main(String[] args) {
    }

    /**
     * 多行輸入元素,其中第一行幾個數字表示下面幾行的個數。
     * // 輸入如下
     * 輸入的數據分別表示的是的數組的大小
     * 3 4
     * 10 2 3
     * 11 4 5 6
     */
    public static void test1() {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int[] num1 = new int[m];
            int[] num2 = new int[n];
            for (int i = 0; i < m; i++) {
                num1[i] = sc.nextInt();
            }
            for (int i = 0; i < n; i++) {
                num2[i] = sc.nextInt();
            }
            //顯示結果的代碼
            System.out.println("輸出:");
            System.out.println(Arrays.toString(num1));
            System.out.println(Arrays.toString(num2));
        }
    }

    /**
     * 在一行輸入多個參數
     * ABB CCC DDD  EEE 123 435
     */
    public static void test2() {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();  // 讀取一行
            String[] strIn = str.trim().split(" ");  // 以空格分割
            System.out.println(Arrays.toString(strIn));//顯示結果
        }
    }

    /**
     * 每行輸入不等數量的參數  存儲的是的數字
     * <p>
     * 3
     * AA bcd 123 54
     * AA BB
     * A B C
     */
    public static void test3() {
        Scanner sc = new Scanner(System.in);
        //一共幾行
        int m = sc.nextInt();
        sc.nextLine();  // 很重要,跳到第二行
        //存放數據的list中的
        ArrayList<int[]> list = new ArrayList<>();

        String[] strArr = new String[m];
        // 從第二行開始讀取  存儲是整形數組
        for (int i = 0; i < m; i++) {
            String[] s = sc.nextLine().split(" ");
            //新建立的數組
            int[] array = new int[s.length];
            //新數組賦值
            for (int j = 0; j < array.length; j++) {
                array[j] = Integer.valueOf(s[j]);
            }
            list.add(array);
        }
        //顯示結果
        for (int[] s : list) {
            for (int v : s) {
                System.out.print(v + "--");
            }
            System.out.println();
        }
    }

    /**
     * 第一行是是表示的數據的行數 第二是表示的每一行的數據
     * 3
     * 5 6 4
     * 5 69 87 78
     * 8 9 7 2
     */
    public static void test4() {
        Scanner sc = new Scanner(System.in);
        //讀取幾行停止輸入
        int m = sc.nextInt() - 1;
        //讀取下一行
        sc.nextLine();
        List<String[]> list = new ArrayList<>();

        while (sc.hasNext()) {
            //String str = sc.nextLine();
            String[] s = sc.nextLine().split(" ");
            list.add(s);
            if (m > 0) {
                --m;
            } else {
                break;
            }
        }
        for (String[] s : list) {
            for (int i = 0; i < s.length; i++) {
                System.out.print(s[i] + " ");
            }
            System.out.println();
        }
    }

    /**
     * java中從控制檯輸入多行數據 按回車鍵輸入空行結束
     * 4
     * 5
     * 6
     * 空格
     */
    public static void test5() {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        do {
            //讀取到的每一行的字符串
            String string = sc.nextLine();
            if (string.equals("")) {
                break;
            }
            list.add(Integer.valueOf(string));
        } while (true);
        //顯示結果
        for (int v : list) {
            System.out.println(v);
        }
    }

    /**
     * 每行輸入不等數量的參數 下面每一行都是存儲的是的字符串
     * <p>
     * 3
     * AA bcd 123 54
     * AA BB
     * A B C
     */
    public static void test6() {
        Scanner sc = new Scanner(System.in);
        //一共幾行
        int m = sc.nextInt();
        sc.nextLine();  // 很重要,跳到第二行
        //字符串數組
        ArrayList<String[]> list2 = new ArrayList<>();
        // 從第二行開始讀取  存取的是的String類型的數組
        for (int i = 0; i < m; i++) {
            String[] s = sc.nextLine().split(" ");
            list2.add(s);
        }
        //顯示結果
        for (String[] ss : list2) {
            for (String s : ss) {
                System.out.print(s + "--");
            }
            System.out.println();
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章