JAVA进阶16

间歇性混吃等死,持续性踌躇满志系列-------------第16天

1、桌球游戏小项目

①窗口加载

 1 import javax.swing.*;
 2 
 3 public class BallGame extends JFrame {
 4 
 5     //窗口加载
 6     void launchFrame() {
 7         setSize(300, 300);
 8         setLocation(100, 100);
 9         setVisible(true);
10     }
11 
12     public static void main(String[] args) {
13         System.out.println("游戏体验");
14         BallGame game = new BallGame();
15         game.launchFrame();
16     }
17 }
窗口加载

运行结果图

②小球左右移动

注:图片加载不出可尝试用绝对路径

 1 import javax.swing.*;
 2 import java.awt.*;
 3 
 4 public class BallGame extends JFrame {
 5 
 6     Image ball = Toolkit.getDefaultToolkit().getImage("F:/Java/MyGame/Demo01/images/ball.png");
 7     Image desk = Toolkit.getDefaultToolkit().getImage("F:/Java/MyGame/Demo01/images/desk.jpg");
 8     //小球的横坐标
 9     double x = 100;
10     //小球的纵坐标
11     double y = 100;
12     //方向
13     boolean right = true;
14 
15     //画窗口的方法
16     public void paint(Graphics g) {
17         System.out.println("窗体被画了一次!");
18         g.drawImage(desk, 0, 0, null);
19         g.drawImage(ball, (int) x, (int) y, null);
20 
21         if (right) {
22             x += 10;
23         } else {
24             x -= 10;
25         }
26         //856是窗口宽度,40是桌子边框的宽度,30是小球的直径
27         if (x > 856-40-30) {
28             right = false;
29         }
30         //40是桌子边框的宽度
31         if (x < 40) {
32             right = true;
33         }
34     }
35 
36     //窗口加载
37     void launchFrame() {
38         setSize(856, 500);
39         setLocation(50, 50);
40         setVisible(true);
41 
42         //重画窗口,每秒画25次
43         while (true) {
44             repaint();
45             try {
46                 Thread.sleep(40);
47             } catch (Exception e) {
48                 e.printStackTrace();
49             }
50 
51         }
52     }
53 
54     public static void main(String[] args) {
55         System.out.println("游戏体验");
56         BallGame game = new BallGame();
57         game.launchFrame();
58     }
59 }
小球左右移动

运行结果图

③实现圆球上下弹动

 1 import javax.swing.*;
 2 import java.awt.*;
 3 
 4 public class BallGame01 extends JFrame {
 5 
 6     Image ball = Toolkit.getDefaultToolkit().getImage("F:/Java/MyGame/Demo01/images/ball.png");
 7     Image desk = Toolkit.getDefaultToolkit().getImage("F:/Java/MyGame/Demo01/images/desk.jpg");
 8     //小球的横坐标
 9     double x = 100;
10     //小球的纵坐标
11     double y = 100;
12 
13     //弧度,此处就是60度
14     double degree = 3.14 / 3;
15 
16     //画窗口的方法
17     public void paint(Graphics g) {
18         System.out.println("窗体被画了一次!");
19         g.drawImage(desk, 0, 0, null);
20         g.drawImage(ball, (int) x, (int) y, null);
21 
22         x = x + 10 * Math.cos(degree);
23         y = y + Math.sin(degree);
24 
25         if (y > 500 - 40 - 30 || y < 40) {
26             degree = -degree;
27         }
28         if (x < 0 || x > 856) {
29             degree = 3.14 - degree;
30         }
31     }
32 
33     //窗口加载
34     void launchFrame() {
35         setSize(856, 500);
36         setLocation(50, 50);
37         setVisible(true);
38 
39         //重画窗口,每秒画25次
40         while (true) {
41             repaint();
42             try {
43                 Thread.sleep(40);
44             } catch (Exception e) {
45                 e.printStackTrace();
46             }
47 
48         }
49     }
50 
51     public static void main(String[] args) {
52         System.out.println("游戏体验");
53         BallGame01 game = new BallGame01();
54         game.launchFrame();
55     }
56 }
View Code

运行结果图

 

2、三元运算符

 1 public class TestOperator01 {
 2     public static void main(String[] args) {
 3 //        boolean b1 = true;
 4 //        boolean b2 = false;
 5 //        System.out.println(b1 & b2);
 6 //        System.out.println(b1 ^ b2);
 7 //        System.out.println(b1 || b2);
 8 //        System.out.println(!b2);
 9 //
10         //三元运算符
11         int score = 80;
12         String type = score<60?"不及格":"及格";
13         System.out.println(type);
14 
15         //条件判断
16         if (score>60){
17             System.out.println("及格");
18         }else {
19             System.out.println("不及格");
20         }
21     }
22 }
View Code

运行结果图

3、嵌套循环

 1 import java.util.Scanner;
 2 
 3 public class TestOperator01 {
 4     public static void main(String[] args) {
 5         for (int i = 1; i <= 5; i++) {
 6             for (int j = 1; j <= 5; j++) {
 7                 System.out.print(i+"\t");
 8             }
 9             System.out.println(" ");
10         }
11     }
12 }
View Code

运行结果图

4、嵌套循环打印九九乘法表

 1 public class TestOperator01 {
 2     public static void main(String[] args) {
 3         for (int i = 1; i < 10; i++) {
 4             for (int j = 1; j <= i; j++) {
 5                 System.out.print(j+"*"+i+"="+i*j+"\t");
 6             }
 7             System.out.println();
 8         }
 9     }
10 }
View Code

运行结果图

5、循环输出1-1000之间能被5整除的数,且每行输出5个

 1 public class TestOperator01 {
 2     public static void main(String[] args) {
 3         int h = 0;
 4         for (int i = 1; i <= 1000; i++) {
 5             if (i%5==0){
 6                 System.out.print(i+"\t");
 7                 h++;
 8             }
 9             if (h==5){
10                 System.out.println();
11                 h=0;
12             }
13         }
14         }
15     }
View Code

运行结果图

猜你喜欢

转载自www.cnblogs.com/Anemia-BOY/p/10644452.html