Java interview written test acm version input

First distinguish scanner.nextInt()//Input an integer, only one number can be read, and the space will stop.

scanner.next()//Input a string, only one string can be read, and the space will stop, but the comma will not stop.

scanner.nextLine() reads one line, stops on newline, does not stop on space

1. Read multiple parameters from the same line

Example: Input: 1, 2

If it is 1 2 (if there is a space in the middle), just read it directly.

If it is 1,2 comma separated,

//Input the following
ABB, CCC, DDD, EEE, 123, 435

Definitely use nextLine,

like:


2 Take three lines of input as an example. In the first line, enter two numbers m and n, which represent the lengths of the arrays num1 and num2 respectively. In the second and third lines, enter the elements of num1 and num2, separated by spaces.

// 输入如下
3 4
10 2 3 
11 4 5 6

Solution:

import java.util.Arrays;
import java.util.Scanner;

public class myScanner {
	Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		System.out.println("输入:");
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		int[] num1 = new int[m];
		int[] num2 = new int[n];
		// 换成其他数据类型也一样,其他数值类型就修改int跟nextInt就可以了,
		//String就把nextInt()换成next()
		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));
	}
}

3. Enter multiple parameters in multiple lines, and the number of parameters in each line is variable.

Assume that m and n are entered in the first line. m means that there are m lines after it, and n means that there are at most n in each line (it can be used to truncate multiple input parameters in a certain line, and I will not analyze it in detail).

// 输入如下
3 4
AA bcd 123 54
AA BB
A B C

Solution:

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

public class myScanner {
	Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		System.out.println("输入:");
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		sc.nextLine();  // 很重要,跳到第二行
		// 若直接确定行数,注释掉上面两行,加入下面一行
		// int m = 3;
		String[] strArr = new String[m];
		// 从第二行开始读取
		for(int i = 0; i < m; i++) {
			strArr[i] = sc.nextLine();
		}
		System.out.println("输出:");
		System.out.println(Arrays.toString(strArr));
		ArrayList<String[]> strToOne = new ArrayList<String[]>();
		for(int i = 0; i < m; i ++) {
			String[] tmp = strArr[i].trim().split(" ");
			strToOne.add(tmp);
		}
		System.out.println(strToOne);
		// 形象点显示
		System.out.print("[");
		for(int i = 0; i < strToOne.size(); i++) {
			System.out.print(Arrays.toString(strToOne.get(i)));
			if(i != strToOne.size()-1)
				System.out.print(", ");
		}
		System.out.print("]");
	}
}

4. Input a string array and convert it to an integer

For example

Input: [1,2,3,4] //The whole is a string

solution:

import java.util.Scanner;

public class myScanner {
    Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String c = in.nextLine();//读取一整行
        c = c.substring(1, c.length() - 1);//去掉两头的[]
        System.out.println(c);
        String[] ch = c.split(",");//按照,分割
        int a[] = new int[ch.length];//定义一个整数数组
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i] + " ");
            a[i] = Integer.valueOf(ch[i]);//将字符串型转换为数组
            System.out.println(a[i]);
        }

    }
}

Guess you like

Origin blog.csdn.net/qq_45874683/article/details/132834244