火星车

 class DirectionKey {//定义一个火星车的对象
     private String up ;//向上 W (y+1)
     private String down;//向下 S (y-1)
     private String left;//向左 A(x-1)
     private String right;//向右 D(x+1)
     private int x;
    private int y;

    // public void setX(int x) {
 //         this.x = x;
 //     }
 //     public int getX() {
 //         return x;
 //     }

 //     public void setY(int y) {
 //         this.y = y;
 //     }
 //     public int getY() {
 //         return y;
 //     }

     public void setUp(String up) {
        if ("W".equals(up)){
             this.y = ++y;
            System.out.println(y+";"+x);
            }
        }

    public String getUp() {
        return up;
    }

    public void setDown(String down) {
        if ("S".equals(down)){
             this.y = --y;
            System.out.println(y+";"+x);
            }
    }
    public String getDown() {
        return down;
    }

    public void setLeft(String left) {
        if ("A".equals(left)){
             this.x = --x;
            System.out.println(y+";"+x);
            }
    }
    public String getLeft() {
        return left;
    }

    public void setRight(String right) {
        if ("D".equals(right)){
             this.x = ++x;
            System.out.println(y+";"+x);
            }
    }
    public String getRight() {
        return right;
    }

    // public void up(String up) {
    //     System.out.println(this.x+";"+this.y);
    // }
}

    // public void up() {
//     //     int a = 
//     //     System.out.println(x+";"+y);
//     // }

// // class coordinate {//坐标
// //     private int x;
//     private int y;

// public void setX(int x) {
//          this.x = x;
//      }
//      public int getX() {
//          return get;
//      }

//      public void setY(int y) {
// //          this.y = y;
// //      }
// //      public int getY() {
// //          return get;

// }

// }

public class Demo {
    public static void main(String args[]) {
        DirectionKey directionkey = new DirectionKey();
         directionkey.setUp("W");
         directionkey.setDown("S");
         directionkey.setLeft("A");
         directionkey.setRight("D");

         // directionkey.up();
        // directionkey.setUp("W");
        // // directionkey.setX(1);
        // // directionkey.setY(0);
        // directionkey.up("2");
    }
}

//字符串.equals( ) 字符串的比较

结果:输入W y+1......

参数: String w  String S  String A String D  int x  int y

private私有 

set方法 4个if方法


 

转载于:https://my.oschina.net/u/3708604/blog/1556287

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值