雙色球問題:機選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.對象都屬于某個類 - 類是造對象的藍圖或模板
- 對象之間是通過互相發消息連接起來的
- 寫代碼的終極原則:高內聚,低耦合
- 面向對象編程的步驟:
- 定義類
數據抽象 - 找到和對象相關的屬性 - 找名詞
行為抽象 - 找到和對象相關的行為(方法) - 找動詞 - 創建對象
new 構造器(); - 給對象發消息
寫構造器,通常是公開的,名字跟類名一樣
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);
}