Java学习1206

2016-12-06  本文已影响0人  a04b20daaf33
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. 对象都有属性和行为
  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);//设置窗口可见的
    }

}

上一篇 下一篇

猜你喜欢

热点阅读