每日要點
方法的重載
方法的重載: 在一個類中可以出現(xiàn)同名方法 只要它們的參數(shù)列表不同就能夠加以區(qū)分闰靴。
參數(shù)列表不同指的是: 參數(shù)的類型不相同或者參數(shù)的個數(shù)不相同或者二者皆不同哲思。
public static double perimeter(double width, double height) {
return (width + height) * 2;
}
public static double area(double width, double height) {
return width * height;
}
public static double perimeter(double a, double b, double c) {
return a + b + c;
}
public static double area(double a, double b, double c) {
double half = perimeter(a, b, c) /2;
return Math.sqrt(half * (half - a) * (half - b) * (half - c));
}
引用其他類的方法
靜態(tài)導(dǎo)入(不常用的語法)
import static com.jack.Test02.*;
類名.方法
double fencePrice = Test02.perimeter(r + 3) * FENCE_UNIT_PRICE;
數(shù)組
數(shù)組 - 用一個變量保存多個同種類型的值
可以用以下來創(chuàng)建數(shù)組
int[] f = { 0, 0, 0, 0, 0, 0};
int[] f = new int[6];
數(shù)組的length屬性
f.length
表示這個數(shù)組的長度
for-each循環(huán) (Java 5+)
給數(shù)組每一個找一個代言人央勒,如循環(huán)開始 int i
來 代替 f[0]
寺滚,循環(huán)一次 代替下一個數(shù)
只讀循環(huán)
for (int i : f) {
System.out.println(i);
}
排序
- 冒泡排序: 每2個進行比較斑鼻,大的往后走喻圃,小的往前走萤彩。循環(huán)一次找到一個最大
public class Test06 {
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 + " ");
}
}
public static void bubbleSort(int[] array) {
boolean swapped = true;
// 冒泡排序
for (int i = 1; swapped && i <= array.length - 1; 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;
}
}
}
}
- 選擇排序:第一個與后面所有數(shù)比較玖姑,選出最小的放在第一位
周末作業(yè)講解
- 1. 5個人去打魚裹驰,打了很多魚
a、b荒叶、c肆汹、d愚墓、ea 先醒過來,分5份昂勉,多了一條浪册,扔掉,拿走自己的一份
b 然后醒過來岗照,分5份村象,多了一條笆环,扔掉,拿走自己的一份
...
最少要捕多少魚
for (int i = 1; ; i++) {
int total = i;
boolean isEnough = true;
for (int j = 1; j <= 5; j++) {
if ((total - 1) % 5 == 0) {
total = (total - 1) / 5 * 4;
}
else {
isEnough = false;
break;
}
}
if (isEnough) {
System.out.println(i);
break;
}
}
練習(xí)
- 1.求過道和圍墻花費多少錢
練習(xí)1.png
初始:
final double PI = 3.14;
Scanner input = new Scanner(System.in);
System.out.print("請輸入游泳池的半徑(m): ");
double r1 = input.nextDouble();
double r2 = r1 + 3;
double area = r2 * r2 * PI - r1 * r1 * PI;
double perimeter = 2 * r2 * PI;
double total = area * 38.5 + 15.5 * perimeter;
System.out.printf("圍墻花費%.2f元,過道花費%.2f元,總共花費%.2f元.",
perimeter * 15.5, area * 38.5, total);
input.close();
修改:
Scanner input = new Scanner(System.in);
System.out.print("請輸入游泳池的半徑: ");
double r = input.nextDouble();
if (r > 0) {
double fencePrice = perimeter(r + 3) * FENCE_UNIT_PRICE;
double aislePrice = (area(r + 3) -
area(r)) * AISLE_UNIT_PRICE;
System.out.printf("圍墻的造價為: %.2f元\n", fencePrice);
System.out.printf("過道的造價: %.2f元", aislePrice);
}
else {
System.out.println("游泳池的半徑應(yīng)該是一個正數(shù).");
}
input.close();
}
public static double perimeter(double radius) {
return 2 * Math.PI * radius;
}
public static double area(double radius) {
return Math.PI * radius * radius;
}
-
2. 排列數(shù): A(m,n) = m! / n!
計算組合數(shù): C(m,n) = m! / n! / (m -n)!
首先:
Scanner input = new Scanner(System.in);
System.out.print("m = ");
int m = input.nextInt();
System.out.println("n = ");
int n = input.nextInt();
if (m >= n) {
double result = 1;
for (int i = 2; i <= m; i++) {
result *= i;
}
double fm = result;
result = 1;
for (int j = 2; j <= n; j++) {
result *= j;
}
double fn = result;
result = 1;
for (int k = 2; k <= m - n; k++) {
result *= k;
}
double fmn =result;
System.out.printf("C(%d,%d) = %.0f\n",
m, n, fm / fn / fmn);
}
else {
System.out.println("輸入錯誤.");
}
input.close();
修改:
Scanner input = new Scanner(System.in);
System.out.print("m = ");
int m = input.nextInt();
System.out.println("n = ");
int n = input.nextInt();
if (m >= n) {
System.out.printf("C(%d,%d) = %.0f\n",
m, n, f(m) / f(n) / f(m - n));
}
else {
System.err.println("輸入錯誤.");
}
input.close();
}
public static double f(int n) {
double fn =1; // 保存n的階乘的變量
for (int i = 2; i <= n; i++) {
fn *= i;
}
return fn;
}
- 3.有30個人(15個基督教徒和15個非教徒)坐船 船壞 要把15個人扔到海里其他人才能得救
圍成一圈從某個人開始從1報數(shù) 報到9的人扔到海里 下一個人繼續(xù)從1開始報數(shù) 報到9扔到海里
以此類推 直到把15個人扔到海里為止 結(jié)果由于上帝的保佑15個基督教徒都幸免于難
問這些人最初是怎么站的 哪些位置是基督徒 哪些位置是非教徒 --- Josephu環(huán)(約瑟夫環(huán))
boolean[] persons = new boolean[30];
for (int i = 0; i < persons.length; i++) {
persons[i] = true;
}
int counter = 0;
int number = 0;
int index = 0;
while(counter < 15 ) {
if (persons[index]) {
number += 1;
if (number == 9) {
persons[index] = false;
counter += 1;
number = 0;
}
}
index += 1;
if (index == 30) {
index = 0;
}
}
for (boolean b : persons) {
System.out.print(b ? "基" : "非");
}
例子
- 1.使用方法厚者,求三角形和矩形的周長和面積
Scanner input = new Scanner(System.in);
System.out.print("請輸入三角形三條邊的長度: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
if (isValidForTriangle(a, b, c)) {
System.out.println("周長: " + perimeter(a, b, c));
System.out.println("面積: " + area(a, b, c));
}
else {
System.err.println("不能構(gòu)成三角形");
}
System.out.print("請輸入矩形的寬和高: ");
double width = input.nextDouble();
double height = input.nextDouble();
System.out.println("周長: " + perimeter(width, height));
System.out.println("面積: " + area(width, height));
input.close();
}
public static double perimeter(double radius) {
return 2 * Math.PI * radius;
}
public static double area(double radius) {
return Math.PI * radius * radius;
}
// 方法的重載: 在一個類中可以出現(xiàn)同名方法 只要它們的參數(shù)列表不同就能夠加以區(qū)分
// 參數(shù)列表不同指的是: 參數(shù)的類型不相同或者參數(shù)的個數(shù)不相同或者二者皆不同
public static double perimeter(double width, double height) {
return (width + height) * 2;
}
public static double area(double width, double height) {
return width * height;
}
public static double perimeter(double a, double b, double c) {
return a + b + c;
}
public static double area(double a, double b, double c) {
double half = perimeter(a, b, c) /2;
return Math.sqrt(half * (half - a) * (half - b) * (half - c));
}
public static boolean isValidForTriangle(double a, double b, double c) {
return a + b > c && a + c > b && b + c > a;
}
- 2.數(shù)組:搖骰子
int[] f = new int[6];
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 + "點搖出了" + f[i - 1] + "次");
}
- **3.斐波那契 數(shù)列 **
int[] f = new int[20];
f[0] = f[1] = 1;
// f.lenth 屬性
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 1] + f[i - 2];
}
/*for (int i = 0; i < f.length; i++) {
System.out.println(f[i]);
}*/
// for-each循環(huán) (Java 5+)
// 給數(shù)組每一個找一個代言人 如循環(huán)開始 int i 來 代替 f[0] 循環(huán)一次 代替下一個 數(shù)
// 只讀循環(huán)
for (int i : f) {
System.out.println(i);
}
- 4.用一個數(shù)組來存學(xué)生的成績躁劣,算最高和最低分,平均分
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("請輸入" + names[i] + "的成績: ");
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 (double d : scores) {
if (d > max) {
max = d;
}
else if (d < min) {
min = d;
}
}
System.out.println("平均分: " + sum / scores.length);
System.out.println("最高分: " + max + " 最低分: " + min);
作業(yè)
- 1.買彩票
紅色球 1~33 藍色球 1~16
01 05 11 17 18 22 05
08 10 22 23 31 32 11
數(shù)字不能重復(fù)
輸入 幾 隨機選出 幾組號碼
Scanner input = new Scanner(System.in);
System.out.print("你想要幾組號碼,請輸入: ");
int total = input.nextInt();
for (int j = 0; j < total; j++) {
int blueNum = (int) (Math.random() * 16 + 1);
int[] num = new int[5];
for (int i = 0; i < 5;) {
int redNum = (int) (Math.random() * 33 + 1);
if (!existed(num, redNum)) {
num[i] = redNum;
i++;
}
}
bubbleSort(num);
for (int i : num) {
System.out.print(i + ",");
}
System.out.println(blueNum);
}
input.close();
}
public static void bubbleSort(int[] x) {
boolean swapped = true;
for (int i = 1; swapped && i <= x.length - 1; i++) {
swapped = false;
for (int j = 0; j < x.length - i; j++) {
if (x[j] > x[j + 1]) {
int temp = x[j + 1];
x[j + 1] = x[j];
x[j] = temp;
swapped = true;
}
}
}
}
public static boolean existed(int[] x, int a) {
boolean flag = false;
for (int i : x) {
if (a == i) {
flag = true;
break;
}
}
return flag;
}