- 隨機生成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的隨機數(shù)作為紅色球的號碼
int number = (int) (Math.random() * 33 + 1);
// 檢測此號碼在之前選中的號碼中有沒有出現(xiàn)過
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);
}
}
- 二維數(shù)組(楊輝三角)
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();
}
}
}
面向?qū)ο?/h2>
- 對象是能夠接收消息的實體
特征:1.一切都是對象
- 對象都有屬性和行為
- 每一個對象都是第一無二的
- 對象都屬于某個類
- 類:
類是對象的藍圖和模板
對象和對象之間可以互相發(fā)送消息就能構(gòu)造出復(fù)雜的系統(tǒng)
package com.baidu;
// 面向?qū)ο缶幊痰牡?步 - 定義類
public class Student {
// 數(shù)據(jù)抽象 - 屬性 - 找名詞
private String name;
private int age;
//構(gòu)造器
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){
// 面向?qū)ο缶幊痰?步 - 創(chuàng)建對象
// new 構(gòu)造器();
Student stu = new Student("楊海龍", 12);
// 面向?qū)ο缶幊痰?步 - 給對象發(fā)消息
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) {
//創(chuàng)建窗口對象
JFrame f = new JFrame("我的第一個窗口");
// 通過給窗口對象發(fā)消息來設(shè)置和顯示窗口
f.setSize(400, 300);
f.setLocationRelativeTo(null);//設(shè)置窗口居中顯示
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗口關(guān)閉后自動結(jié)束程序
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);//設(shè)置窗口可見的
}
}
特征:1.一切都是對象
類是對象的藍圖和模板
對象和對象之間可以互相發(fā)送消息就能構(gòu)造出復(fù)雜的系統(tǒng)
package com.baidu;
// 面向?qū)ο缶幊痰牡?步 - 定義類
public class Student {
// 數(shù)據(jù)抽象 - 屬性 - 找名詞
private String name;
private int age;
//構(gòu)造器
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){
// 面向?qū)ο缶幊痰?步 - 創(chuàng)建對象
// new 構(gòu)造器();
Student stu = new Student("楊海龍", 12);
// 面向?qū)ο缶幊痰?步 - 給對象發(fā)消息
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) {
//創(chuàng)建窗口對象
JFrame f = new JFrame("我的第一個窗口");
// 通過給窗口對象發(fā)消息來設(shè)置和顯示窗口
f.setSize(400, 300);
f.setLocationRelativeTo(null);//設(shè)置窗口居中顯示
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗口關(guān)閉后自動結(jié)束程序
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);//設(shè)置窗口可見的
}
}