美文网首页
Java学习1206

Java学习1206

作者: a04b20daaf33 | 来源:发表于2016-12-06 20:57 被阅读0次
  • 随机生成n注彩色球,红球6个,蓝色球一个,6个红色球按序排好
package com.baidu;

public class Test01 {
    public static void bubbleSort(int[] array){
        boolean swapped = true;
        for(int i = 1; swapped && i <= array.length;i++){
            for(int j = 0; j < array.length - i;j++){
                if(array[j] > array [j + 1]){
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] redBallsArray = new int[6];
        for (int i = 0; i < redBallsArray.length; ) {
            // 生成1~33的随机数作为红色球的号码
            int number = (int) (Math.random() * 33  + 1);
            // 检测此号码在之前选中的号码中有没有出现过
            boolean isDuplicated = false;
            for (int j = 0; j < i; j++) {
                if(redBallsArray[j] == number){
                    isDuplicated = true;
                    break;
                }
            }
            if(!isDuplicated){
                redBallsArray[i] = number;
                i += 1;
            }
        }
        bubbleSort(redBallsArray);
        for(int x : redBallsArray){
            System.out.printf("%02d ", x );
        }
        int blueBall = (int) (Math.random() * 16 + 1);
        System.out.printf("| %02d\n", blueBall);
    }

}

  • 二维数组(杨辉三角)
package com.baidu;

public class Test02 {

    public static void main(String[] args) {
        int[][] y =new int[10][];
        for (int i = 0; i < y.length; i++) {
            y[i] = new int[i + 1];
            for (int j = 0; j < y[i].length; j++) {
                if(j == 0 || j == i){
                    y[i][j] = 1;
                }
                else{
                    y[i][j] = y[i - 1][j] + y[i - 1][j - 1];
                }
                System.out.print(y[i][j] + "\t");
            }
            System.out.println();
        }
    }

}

面向对象

  • 对象是能够接收消息的实体
    特征:1.一切都是对象
  1. 对象都有属性和行为
  2. 每一个对象都是第一无二的
  3. 对象都属于某个类
  • 类:
    类是对象的蓝图和模板
    对象和对象之间可以互相发送消息就能构造出复杂的系统
package com.baidu;
//  面向对象编程的第1步 - 定义类
public class Student {
    // 数据抽象 - 属性 - 找名词
    private String name;
    private int age;
    //构造器
    public Student(String n, int a){
        name = n;
        age = a;
    }
    
    // 行为抽象 - 方法 - 找动词
    public void play(String gameName){
        System.out.println(name + "正在玩" + gameName + ".");
    }
    public void study(){
        System.out.println(name + "正在学习.");
    }
    public void watchJapaneseAV(){
        if(age > 18){
            System.out.println(name + "正在观看岛国爱情动作片.");
        }
        else{
            System.out.println(name + "只能观看<熊出没>.");
        }
    }
}

package com.baidu;

public class Test04 {
    public static void main(String[] args){
    // 面向对象编程第2步 - 创建对象
    // new  构造器();
        Student stu = new Student("杨海龙", 12);
    //  面向对象编程第3步 - 给对象发消息
        stu.study();
        stu.play("鸡鸡");
        stu.watchJapaneseAV();
        
        Student stu2 = new Student("海龙儿子", 39);
        stu2.watchJapaneseAV();
    }
}

  • 显示窗口,在窗口中显示时间或便签
package com.baidu;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test07 {

    public static void main(String[] args) {
        //创建窗口对象
        JFrame f = new JFrame("我的第一个窗口");
        // 通过给窗口对象发消息来设置和显示窗口
        f.setSize(400, 300);
        f.setLocationRelativeTo(null);//设置窗口居中显示
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭后自动结束程序
        
        Clock clock = new Clock();
        JLabel label = new JLabel(clock.show());
        Font font = new Font("微软雅黑", Font.BOLD, 36);
        label.setFont(font);
        label.setHorizontalAlignment(JLabel.CENTER);
        f.add(label);
        f.setVisible(true);//设置窗口可见的
    }

}

相关文章

  • Java学习1206

    随机生成n注彩色球,红球6个,蓝色球一个,6个红色球按序排好 二维数组(杨辉三角) 面向对象 对象是能够接收消息的...

  • 1206学习总结

    开课准备上午开始班主任跟我们说明了在学习中需要在上嵌教质系统中需要完成的任务:以每周为周期,每周需要完成头脑风暴、...

  • 1206

    我们正在进入一个完全陌生的时代,我们困惑于很多的问题无法用任何的过去的教育和已有知识进行解答。所以我们努力观察和自...

  • 1206

    英文小说:交谈的对话更能让人静心阅读,或许是因为它有语气语调和情感温度。

  • 1206

    只是记录,在2016年的最后一个月

  • 1206

    最近深刻认识到这是一个瞬息万变的时代。如果跟不上别人的步伐只能望其项背。争取每天自己有一点进步。加油~

  • 1206

    今天貌似有好几个人生日,但是没有一个是给我的。有点心酸。

  • 1206

    今天不知道该写什么,就一直拖到现在,老师讲的挺明白,做的也还算凑合吧,就是莫名的烦躁,就一直拖着没写。 也不知道是...

  • 1206

    信任可是两个人在一起的基础 我就是个不会辩解的人 要理解不愿和不敢的区别 文明其精神,野蛮其体魄 洗澡发现,肩膀上...

  • 1206

    the beginning is not easy,always a better way for learnin...

网友评论

      本文标题:Java学习1206

      本文链接:https://www.haomeiwen.com/subject/twgrmttx.html