递归:递归就是自己调自己,优点:方便,缺点:耗资源。
代码:
package com.codeday18.demo02;
public class FatctorialTerst {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
long s = factorial0(20);
long t2 = System.currentTimeMillis();
long dt = t2-t1;
System.out.println("结果是:" + s);
System.out.println("花费了:" + dt + " ms");
}
public static long factorial0(int a) {
if (a == 1) {
return 1;
} else {
return a * factorial0(a - 1);
}
}
}
写的时候发现了静态方法不能调取非静态变量的问题?
飞机游戏:
窗口弹出:
package com.bjsxt.plane;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyGameFrame extends Frame{
public void launchFrame(){
// 打印标题
setTitle("樊睿作品");
// 设窗口为可见
setVisible(true);
// 窗口大小
setSize(500,500);
// 增加关闭监听,在右上角可以关闭窗口
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame();
f.launchFrame();
}
}
Frame
类是java.awt
中的主要类,弄窗口都需要继承Frame。
package plane;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class GameUtil {
private GameUtil(){
}
public static Image getImage(String path){
BufferedImage img = null;
URL u = GameUtil.class.getClassLoader().getResource(path);
try {
img = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(u);
return img;
}
public static void main(String[] args) {
Image img = GameUtil.getImage("images/plane.png");
System.out.println(img);
}
}
这里路径老师有问题,不知道为啥,总显示是null。