美文网首页
Java学习第8天

Java学习第8天

作者: _Raye | 来源:发表于2016-12-06 20:09 被阅读0次
双色球问题:机选n注,前面6位在133中选,且排好序。最后一位在116中选
package org.mobiletrain;

import java.util.Scanner;

public class Test01 {

    private static void bubbleSort(int [] array){
        boolean swapped = true;
        for(int i = 1; swapped && i < array.length; i++){
            swapped = false;
            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) {
        Scanner input = new Scanner(System.in);
        System.out.print("机选几注:");
        int n = input.nextInt();
        input.close();
        for(int counter = 1; counter <= n;counter++){
        int[] redBalls = new int[6];
        for (int i = 0; i < redBalls.length;){
            //生成1~33的随机数,作为红色球的号码
            int number = (int) (Math.random() * 33 + 1);
            //检查此号码在之前选中的号码中有没有出现过
            boolean isDuplicated = false;
            for (int j = 0; j < i; j++){
                //如果当前号码已经出现过
                if (redBalls[j] == number) {
                    isDuplicated = true;
                    break;
                }
            }
            if (!isDuplicated) {
                redBalls[i] = number;
                i++;//只有选中不重复的,i才+1.
            }
        }
        bubbleSort(redBalls);
        for(int x: redBalls){
            System.out.printf("%02d ", x);
        }
        int blueBall = (int) (Math.random() * 16 + 1);
        System.out.printf("(%02d)\n",blueBall);//%02d表示,数字不够两位,前面补0
        }
    }
}

1. 二维数组

  • 可以看成是数组的数组
  • 例如:String str[][] = new String[3][4];
public static void main(String[] args) {
        int[][] scores = new int[5][3];
        for (int i = 0; i < scores.length; i++){
            for (int j = 0; j < scores[i].length; j++){
                scores[i][j] = (int) (Math.random() * 101);
                System.out.print(scores[i][j] + "\t");
            }
            System.out.println();
        }
    }

2. 二维数组的实例:杨辉三角

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();
        }
    }

3. 面向对象

  • 对象 - 能够接收消息的实体
  • 对象的特点:
    1.万物皆对象
    2.每个对象都是独一无二的
    3.每个对象都有自己的属性
    4.对象都属于某个类
  • 类是造对象的蓝图或模板
  • 对象之间是通过互相发消息连接起来的
  • 写代码的终极原则:高内聚,低耦合
  • 面向对象编程的步骤:
  1. 定义类
    数据抽象 - 找到和对象相关的属性 - 找名词
    行为抽象 - 找到和对象相关的行为(方法) - 找动词
  2. 创建对象
    new 构造器();
  3. 给对象发消息
    写构造器,通常是公开的,名字跟类名一样
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 + "只能观看<熊出没>");
        }
     }  
}

使用时:

public static void main(String[] args) {
        //面向对象编程的第2步 --- 创建对象
        //new 构造器();
    Student stu = new Student("王大锤", 15);
        //面向对象编程的第3步 --- 给对象发消息
        stu.study();
        stu.play("LOL");
        stu.watchJapaneseAV();
        
        Student stu2 = new Student("jack", 50);
        stu2.study();
        stu2.play("斗地主");
        stu2.watchJapaneseAV();
    }

4. 面向对象的实例

public class Clock {

    private int hour;
    private int minute;
    private int second;
//  private boolean flag; //默认值:false
//  private double x;//默认值:0.0
//  private String a;//默认值:null
    
    //如果定义类时没写任一个构造器,那么系统会自动添加一个默认的隐式构造器(平庸构造器)什么都不干
    //时钟构造器
    public Clock(){
        //Java 7使用
//      Calendar cal = new GregorianCalendar();
//      this.hour = cal.get(Calendar.HOUR_OF_DAY);
//      this.minute = cal.get(Calendar.MINUTE);
//      this.second = cal.get(Calendar.SECOND);
        //Java 8以后使用
        LocalDateTime time = LocalDateTime.now();
        this.hour = time.getHour();
        this.minute = time.getMinute();
        this.second = time.getSecond();
    }
    
    public Clock(int hour,int minute,int second){
        this.hour = hour;
        this.minute = second;
        this.second = second;
    }
    
    public String display(){
        //这样写代码就和控制台紧紧耦合在了一起
        //System.out.printf("%02d:%02d:%02d\n",hour,minute,second);
        return String.format("%02d:%02d:%02d",hour,minute,second);
    }
    
    public void run() {
        second += 1;
        if (second == 60) {
            second = 0;
            minute += 1;
            if (minute == 60) {
                minute = 0;
                hour += 1;
                if (hour == 24) {
                    hour = 0;
                }
            }
        }
    }
}
  • 使用时:
public static void main(String[] args) {
        Clock clock = new Clock();
        System.out.println(clock.display());
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            clock.run();
            System.out.println(clock.display());
        }
    }

5. Java中自带的窗口类JFrame

public static void main(String[] args) {
        //创建窗口对象
        JFrame frame = new JFrame("我的第一个窗口");
        //通过给窗口对象发消息来设置和显示窗口
        frame.setSize(400,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        Clock clock = new Clock();
        JLabel label = new JLabel(clock.display());
        label.setHorizontalAlignment(JLabel.CENTER);
        Font font = new Font("华文新魏",Font.BOLD,36);
        label.setFont(font);
        frame.add(label);
        
        frame.setVisible(true);
    }

相关文章

网友评论

      本文标题:Java学习第8天

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