JAVA进阶10

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

1、Random

 1 package cn.intcast.day08.demo01;
 2 
 3 import java.util.Random;
 4 
 5 public class Demo02Random {
 6     public static void main(String[] args) {
 7         Random r = new Random();
 8         int s = r.nextInt();
 9         System.out.println("生成的随机数为:"+s);
10     }
11 }
View Code

运行结果图

2、JFrame框架窗体

 1 package code0320;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 //定义一个类继承JFrame类
 7 public class Example1  extends JFrame {
 8     //定义一个CreateJFrame()方法
 9     public void CreateJFrame(String title){
10         //实例化一个JFrame对象
11         JFrame jf = new JFrame(title);
12         //获取一个容器
13         Container container = jf.getContentPane();
14         //创建一个JLabel标签
15         JLabel jl = new JLabel("这是一个JFrame窗体");
16         //使标签上的文字居中
17         jl.setHorizontalAlignment(SwingConstants.CENTER);
18         //将标签添加到容器中
19         container.add(jl);
20         //设置容器的背景颜色
21         container.setBackground(Color.white);
22         //使窗体可视
23         jf.setVisible(true);
24         //设置窗体大小
25         jf.setSize(400,350);
26         //设置窗体关闭方式
27         jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
28     }
29     //在主方法中调用CreateJFrame()方法
30     public static void main(String[] args) {
31         new Example1().CreateJFrame("JFrame窗体");
32     }
33 }
View Code

运行结果图

 

 3、Random:猜数字小游戏

 1 package cn.intcast.day08.demo01;
 2 
 3 /*
 4  *思路
 5  * 1 首先需要产生一个随机数,并且一旦产生不再变化
 6  * 2 需要键盘输入,所以用到了Scanner
 7  * 3 获取键盘输入的数字,用Scanner当中的nextInt方法
 8  * 4 已经得到了两个数字,判断(if)一下,
 9  *   如果太大了,提示太大,并重试
10  *   如果太小,提示太小,并重试
11  * 5 重试就是再来一次,循环次数不确定,用while(true)
12  *
13  */
14 
15 
16 import java.util.Random;
17 import java.util.Scanner;
18 
19 public class Demo02Random {
20     public static void main(String[] args) {
21         Random r = new Random();
22         int num = r.nextInt(100)+1;
23         Scanner s = new Scanner(System.in);
24         while (true){
25             //键盘输入猜测的数字
26             System.out.println("请输入你猜测的数字:");
27             int num1 = s.nextInt();
28             if (num<num1){
29                 System.out.println("太大,重试");
30             }else if (num>num1){
31                 System.out.println("太小,重试");
32             }else {
33                 System.out.println("恭喜你,猜中了!!!");
34                 //如果猜中,不再重试
35                 break;
36             }
37         }
38         System.out.println("游戏结束。");
39     }
40 }
View Code

运行结果图

4、ArrayList集合

 1 package cn.intcast.demo10;
 2 
 3 public class Person {
 4     private String name;
 5     private int age;
 6     private String sex;
 7 
 8     //无参构造
 9     public Person() {
10     }
11 
12     //全参构造
13     public Person(String name, int age, String sex) {
14         this.name = name;
15         this.age = age;
16         this.sex = sex;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public int getAge() {
28         return age;
29     }
30 
31     public void setAge(int age) {
32         this.age = age;
33     }
34 
35     public String getSex() {
36         return sex;
37     }
38 
39     public void setSex(String sex) {
40         this.sex = sex;
41     }
42 }
Person类
 1 package cn.intcast.demo10;
 2 /*
 3 * 题目:
 4 *       定义一个数组,用来存储3个person对象
 5 *
 6 * 数组有一个缺点:一旦创建,则程序运行期间长度不可以发生改变
 7 */
 8 public class Array01 {
 9     public static void main(String[] args) {
10         //首先创建一个长度为3的数组,里面用来存放person类型的对象
11         Person [] array = new Person[4];
12         Person one = new Person("林丹",24,"男");
13         Person two = new Person("姚明",25,"男");
14         Person three = new Person("詹姆斯",26,"男");
15 
16         //将one当中的地址值赋值到数组的0号元素位置
17         array[0] = one;
18         array[1] = two;
19         array[2] = three;
20 
21         System.out.println(array[0].getName());
22         System.out.println(array[1].getName());
23         System.out.println(array[2].getName());
24     }
25 }
数组

运行结果图

原文地址:https://www.cnblogs.com/Anemia-BOY/p/10569079.html