- Java中創(chuàng)建包,一半用公司域名倒過(guò)來(lái)寫(xiě)的方法進(jìn)行命名磷支。
- 例如:package org.mobiletrain; (千鋒的域名) --- 相當(dāng)于用文件夾管理源代碼
- Java方法的重載
- 定義:在一個(gè)類(lèi)中可以出現(xiàn)同名方法棺棵,只要它們的參數(shù)列表不同就能加以區(qū)分楼咳。
- 參數(shù)列表不同是指:參數(shù)的類(lèi)型不相同或者參數(shù)的個(gè)數(shù)不相同或者二者皆不同
- 方法重載的具體表現(xiàn):
- 方法名相同
- 方法的參數(shù)類(lèi)型,個(gè)數(shù)順序至少有一項(xiàng)不同
- 方法的返回類(lèi)型可以不相同
- 方法的修飾符可以不相同
private static final double AISLE_UNIT_PRICE = 38.5;
private static final double F_UNIT_PRICE = 15.5;
//LAP - 最小驚訝原則
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("請(qǐng)輸入游泳池半徑:");
double radius = input.nextDouble();
if (radius > 0) {
System.out.printf("跑道的總造價(jià)為:¥%.2f元\n",total(radius));
}
else {
System.out.println("游泳池的半徑應(yīng)該是一個(gè)正整數(shù)");
}
input.close();
}
public static double area( double radius) {
return Math.PI * radius *radius;
}
public static double perimeter (double radius) {
return 2 * Math.PI * radius;
}
public static double total(double radius) {
double asislePrice = (area(radius + 3) - area(radius))* AISLE_UNIT_PRICE;
double fencePrice = perimeter(radius) * F_UNIT_PRICE;
double totalmoney = asislePrice + fencePrice;
return totalmoney;
}
- 方法使用的實(shí)例
//計(jì)算組合數(shù):C(m,n) = m! / n! * (m - n)!
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("請(qǐng)輸入m:");
int m = input.nextInt();
System.out.print("請(qǐng)輸入n:");
int n = input.nextInt();
if (m >= n) {
System.out.printf("組合數(shù)C(%d,%d) = %.0f",m,n,f(m) / f(n) / f(m - n));
}
else {
System.err.println("輸入錯(cuò)誤V蛐簟E老稹!");
}
input.close();
}
public static double f(int n) {
double fn = 1;
for(int i = 2;i <= n;i++){
fn *= i;
}
return fn;
}
- 數(shù)組
- 定義:用一個(gè)變量保存多個(gè)同種類(lèi)型的值
- 數(shù)組的下標(biāo)是從0開(kāi)始
public static void main(String[] args) {
int [] f = {0,0,0,0,0,0};//int[] f = new int[6]; 表示數(shù)組里面有6個(gè)存儲(chǔ)空間
for(int i = 1;i <= 60000;i++){
int face = (int) (Math.random() * 6 + 1);
f[face - 1] += 1;
}
for(int i = 1;i <= 6; i++){
System.out.println(i + "點(diǎn)出現(xiàn)了" + f[i - 1] + "次");
}
}
- 斐波拉契數(shù)列
- for-each循環(huán)(Java 5+):for(long x: f)
- 這是只讀循環(huán)棒动,過(guò)程中不能修改其他值(讀取的時(shí)候使用)
public static void main(String[] args) {
long[] f = new long[50];
f[0] = f[1] = 1;
for (int i = 2; i < f.length; i++){//f.length表示數(shù)組的長(zhǎng)度
f[i] = f[i - 1] + f[i - 2];
}
for (int i = 0;i < f.length;i++){
System.out.println(f[i]);
}
}
- 數(shù)組的練習(xí)
public static void main(String[] args) {
String[] names = {"關(guān)羽","張飛","黃忠","趙云","馬超"};
double[] scores = new double[names.length];
Scanner input = new Scanner(System.in);
for(int i = 0;i < scores.length;i++){
System.out.print("請(qǐng)輸入" + names[i] + "的成績(jī):");
scores[i] = input.nextDouble();
}
input.close();
double sum = 0;
for (int i = 0; i < scores.length;i++){
sum += scores[i];
}
double max = scores[0];
double min = scores[0];
for(int i = 1; i < scores.length;i++){
if(scores[i] > max){
max = scores[i];
}
else if (scores[i] < min) {
min = scores[i];
}
}
System.out.println("平均分:" + sum / scores.length);
System.out.println("最高分為:" + max);
System.out.println("最低分為:" + min);
}
- Josephu環(huán)(約瑟夫環(huán))
//練習(xí):有30個(gè)人(15個(gè)基督教徒糙申,15個(gè)非教徒)坐船,船壞了船惨,要把15個(gè)人扔到海里柜裸,其他人才能得救
//圍成一個(gè)圈,從某個(gè)人開(kāi)始從1開(kāi)始報(bào)數(shù)粱锐,報(bào)到9的人扔到海里疙挺,下一個(gè)繼續(xù)從1開(kāi)始報(bào)數(shù)
//直到把15個(gè)人扔到海里為止,結(jié)果15個(gè)基督教徒都幸免于難
//問(wèn)這些人是怎么站的怜浅,哪些位置是基督教徒铐然,哪些是非教徒 ---Josephu環(huán)(約瑟夫環(huán))
public class Test06 {
public static void main(String[] args) {
boolean[] persons = new boolean[30];//默認(rèn)值是false
for(int i = 0; i < persons.length; i++){
persons[i] = true;
}
int counter = 0;//弄死多少個(gè)人
int index = 0;//操作數(shù)組的下標(biāo)
int number = 0;//報(bào)數(shù)的值
while (counter < 15) {
if (persons[index]) {
number += 1;
if (number == 9) {
persons[index] = false;
counter += 1;
number = 0;
}
}
index += 1;
index %= persons.length;//防止越界
}
for (boolean isChrist: persons){
System.out.print(isChrist ? "基" : "非");
}
}
}
- 冒泡排序
public static void main(String[] args) {
int[] x = {23, 67, 12, 99, 58, 77, 88, 4, 45, 81};
bubblesort(x);
for (int a: x) {
System.out.print(a + " ");
}
}
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]) {
//交換兩個(gè)元素
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
}
}