2023/6/6总结

常用API

Java常用API String、StringBuilder、Math、System、Object、Objects、BigDecimal等

Math类

Math中没有构造方法,类的成员都是静态的(static修饰),通过类名就可以直接调用

常用方法: 

 示例代码:

public class MathDemo {
    public static void main(String[] args) {
        //1、public static int abs(int a)	获取参数a的绝对值
        System.out.println(Math.abs(88)); //88
        System.out.println(Math.abs(-88)); //88

        //2、public static double ceil(double a)	向上取整
        System.out.println(Math.ceil(12.34)); //13.0
        System.out.println(Math.ceil(12.56)); //13.0

        //3、public static double floor(double a)	向下取整
        System.out.println(Math.floor(12.34)); //12.0
        System.out.println(Math.floor(12.56)); //12.0

        //4、public static long round(double a)	四舍五入取整
        System.out.println(Math.round(12.34)); //12
        System.out.println(Math.round(12.56)); //13

        //5、public static int max(int a,int b)	返回两个数中较大值
        System.out.println(Math.max(66,88)); //88

        //6、public static int min(int a,int b)	返回两个数中较小值
        System.out.println(Math.min(66,88)); //66

        //7、public static double pow(double a,double b)	获取a的b次幂
        System.out.println(Math.pow(2.0,3.0)); //8.0

        //8、public static double random()	返回值为double类型随机数 [0.0~1.0)
        System.out.println(Math.random()); //0.36896250602163483
        System.out.println(Math.random()); //0.3507783145075083
    }
}

 System类

System被静态修饰,通过类名访问

 常用方法:

Arrays类

Arrays类包含用于操作数组的各种方法

工具类的设计:

思想构造方法用 private 修饰(防止外界创建对象)

成员用 public static 修饰(使用类名来访问)

 常用方法:

今天继续写了一下拼图小游戏的项目代码,主要完成了移动图片和判断胜负等功能 

代码如下:

移动图片和查看完整图片,主要用到了上次学的监听的知识点。

 //按下不松时会调用该方法
    @Override
    public void keyPressed(KeyEvent e) {
        int code=e.getKeyCode();
        if(code==65){
            //把界面中所有的图片全部删除
            this.getContentPane().removeAll();
            //加载第一张完整的图片
            JLabel all=new JLabel(new ImageIcon(path+"all.jpg"));
            all.setBounds(83,134,420,420);
            this.getContentPane().add(all);
            //加载背景图片
            //添加背景图片
            JLabel background = new JLabel(new ImageIcon("D:\\java\\PTgame\\image\\background.png"));
            background.setBounds(40, 40, 508, 560);
            //把背景图片添加到界面中
            this.getContentPane().add(background);

            //刷新界面
            this.getContentPane().repaint();

        }
    }

    //松开按键时调用此方法
    @Override
    public void keyReleased(KeyEvent e) {
        //判断游戏是否胜利,如果胜利,此方法需要直接结束,不能再执行下面的移动代码
        if(victory()){
            //返回结果或结束方法
            return;
        }
        //对上下左右进行判断
        //上:38,下:40,左:37,右:39
        int code = e.getKeyCode();
        //System.out.println(code);
        //左,上,右,下
        if (code == 37) {
            if(y==3){
                return;
            }
            data[x][y]=data[x][y+1];
            data[x][y+1]=0;
            y++;
            step++;
            InitImage();
        } else if (code == 38) {
            if(x==3){
                //空白方块已经在最下方,他的下面没有图片能移动了
                return;
            }
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0;
            x++;
            step++;
            //调用方法按照最新的数字加载图片
            InitImage();
        } else if (code == 39) {
            if(y==0){
                return;
            }
            data[x][y] = data[x][y-1];
            data[x][y-1] = 0;
            y--;
            step++;
            //调用方法按照最新的数字加载图片
            InitImage();

        } else if (code == 40) {
            if (x == 0) {
                return;
            }
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            x--;
            step++;
            //调用方法按照最新的数字加载图片
            InitImage();

        }
            else if(code==65){
                InitImage();
            }
            else if(code==87) {
            data = new int[][]{
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
            InitImage();
        }
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值