字符串分割,以及字符串匹配

来源:互联网 发布:me域名多少钱 编辑:程序博客网 时间:2024/06/07 23:37
package cn.jjs.test;


import java.util.*;
public class Main{
     
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String str=sc.nextLine();
            String[] A=str.split(";");
            int x=0,y=0;
            for(String string:A){
            if(string.length() > 0)
            {
            if(string.charAt(0)=='D' && string.substring(1).matches("[0-9]+"))
                        x+=Integer.parseInt(string.substring(1));
                     
                    if(string.charAt(0)=='W' && string.substring(1).matches("[0-9]+"))
                        y+=Integer.parseInt(string.substring(1));
                     
                    if(string.charAt(0)=='S' && string.substring(1).matches("[0-9]+"))
                        y-=Integer.parseInt(string.substring(1));
                     
                    if(string.charAt(0)=='A' && string.substring(1).matches("[0-9]+"))
                        x-=Integer.parseInt(string.substring(1));
                   
            }
                
            }
            System.out.println(x+","+y);
        }
         
        sc.close();
    }
     
   
}
0 0
原创粉丝点击