今天開始學Java 開發一個座標計算工具__線上編程

來源:互聯網
上載者:User
題目描述

開發一個座標計算工具, A表示向左移動,D表示向右移動,W表示向上移動,S表示向下移動。從(0,0)點開始移動,從輸入字串裡面讀取一些座標,並將最終輸入結果輸出到輸出檔案裡面。

 

輸入:

 

合法座標為A(或者D或者W或者S) + 數字(兩位以內)

 

座標之間以;分隔。

 

非法座標點需要進行丟棄。如AA10;  A1A;  $%$;  YAD; 等。

 

下面是一個簡單的例子 如:

 

A10;S20;W10;D30;X;A1A;B10A11;;A10;

 

處理過程:

 

起點(0,0)

 

+   A10   =  (-10,0)

 

+   S20   =  (-10,-20)

 

+   W10  =  (-10,-10)

 

+   D30  =  (20,-10)

 

+   x    =  無效

 

+   A1A   =  無效

 

+   B10A11   =  無效

 

+  一個空 不影響

 

+   A10  =  (10,-10)

 

 

 

結果 (10, -10)


輸入描述:


一行字串 輸出描述:


最終座標,以,分隔 樣本1 輸入

A10;S20;W10;D30;X;A1A;B10A11;;A10;
輸出
10,-10

這個程式看說明感覺挺難的,但是相同了就很簡單,就只是一些簡單的加法操作。

public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
           String s = sc.nextLine();
            String[] a  = s.split(";");
            int x=0;
            int y=0;
            for(int i = 0;i<a.length;i++){
                if(a[i].charAt(0)=='A'&&a[i].substring(1).matches("[0-9]{1,2}")){
                     x-= Integer.parseInt(a[i].substring(1));
                }
                if(a[i].charAt(0)=='S'&&a[i].substring(1).matches("[0-9]{1,2}")){
                    y-=Integer.parseInt(a[i].substring(1));
                }
                if(a[i].charAt(0)=='W'&&a[i].substring(1).matches("[0-9]{1,2}")){
                    y+=Integer.parseInt(a[i].substring(1));
                }
                if(a[i].charAt(0)=='D'&&a[i].substring(1).matches("[0-9]{1,2}")){
                    x+=Integer.parseInt(a[i].substring(1));
                }
            }
            System.out.println(x+","+y);
    }
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.