先读进字符串,然后再用split分割,注意当默认间隔符有其他意义时,要用\转义字符转义
这道题是pat的一道题,主要读入方法看前面就行了
1 import java.util.Scanner; 2 3 public class A { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner keyIn = new Scanner(System.in); 8 9 while(keyIn.hasNext()) { 10 String s = keyIn.nextLine(); 11 String[] ss = s.split(" "); 12 String[] ss0 = ss[0].split("\\."); 13 String[] ss1 = ss[1].split("\\."); 14 int[] a = new int[3]; 15 int[] b = new int[3]; 16 for(int i = 0;i< 3;i++) { 17 a[i] = Integer.parseInt(ss0[i]); 18 } 19 for(int i = 0;i< 3;i++) { 20 b[i] = Integer.parseInt(ss1[i]); 21 } 22 int[] c = new int[3]; 23 int[] d = new int[3]; 24 d[1] = 17; 25 d[2] = 29; 26 for(int i = 2;i >=1;i--){ 27 c[i] += a[i] + b[i]; 28 if(c[i] >= d[i]) { 29 c[i-1] = c[i]/d[i]; 30 c[i] = c[i]%d[i]; 31 } 32 } 33 c[0] += a[0] + b[0]; 34 System.out.println(c[0] + "." + c[1] + "." + c[2]); 35 } 36 37 } 38 39 }