学习了java中int的分装类integer的使用方法。
int和integer不同,integer是int的扩展,integer是int的包装类,里面定义了一些方法。
今天要做的是输入输入两个数字N和M。N代表输入多少字符串。
输入样例为:
4 3 ship sheep sinking thinking thinking sinking
计划将左边的单词和右边的单词放入两个字符串数组里面,具体代码如下:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // System.out.println("输入数字T"); // int T = new Integer(scan.nextLine()); System.out.println("输入数字N,M"); // int N = new Integer(scan.nextLine()); // int M = new Integer(scan.nextLine()); String num = scan.nextLine(); //输入N和M String[] nums = num.split(" "); //将字符串用空格分隔 int N = Integer.parseInt(nums[0]); //将第一个数字存入N int M = Integer.parseInt(nums[1]); //将第二个数字存入M List str1 = new ArrayList(); //定义两个连表 List str2 = new ArrayList(); for(int i = 0, a = 0; i < N - 1 ; i++){ String str = scan.nextLine(); String[] strs = str.split(" "); str1.add(strs[a]); //将字符串放入两个数组 str2.add(strs[a+1]); } System.out.println("list1:" + str1); System.out.println("list2:" + str2); } }
输入输出样例:
输入数字N,M 4 3 ship sheep sinking thinking thinking sinking list1:[ship, sinking, thinking] list2:[sheep, thinking, sinking]