每日要点
方法的重载
方法的重载: 在一个类中可以出现同名方法 只要它们的参数列表不同就能够加以区分。
参数列表不同指的是: 参数的类型不相同或者参数的个数不相同或者二者皆不同。
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));
}
引用其他类的方法
静态导入(不常用的语法)
import static com.jack.Test02.*;
类名.方法
double fencePrice = Test02.perimeter(r + 3) * FENCE_UNIT_PRICE;
数组
数组 - 用一个变量保存多个同种类型的值
可以用以下来创建数组
int[] f = { 0, 0, 0, 0, 0, 0};
int[] f = new int[6];
数组的length属性
f.length
表示这个数组的长度
for-each循环 (Java 5+)
给数组每一个找一个代言人,如循环开始 int i
来 代替 f[0]
,循环一次 代替下一个数
只读循环
for (int i : f) {
System.out.println(i);
}
排序
- 冒泡排序: 每2个进行比较,大的往后走,小的往前走。循环一次找到一个最大
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;
}
}
}
}
- 选择排序:第一个与后面所有数比较,选出最小的放在第一位
周末作业讲解
- 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;
}
}
练习
- 1.求过道和围墙花费多少钱
初始:
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("游泳池的半径应该是一个正数.");
}
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. 排列数: A(m,n) = m! / n!
计算组合数: 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报数 报到9的人扔到海里 下一个人继续从1开始报数 报到9扔到海里
以此类推 直到把15个人扔到海里为止 结果由于上帝的保佑15个基督教徒都幸免于难
问这些人最初是怎么站的 哪些位置是基督徒 哪些位置是非教徒 --- Josephu环(约瑟夫环)
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("不能构成三角形");
}
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;
}
// 方法的重载: 在一个类中可以出现同名方法 只要它们的参数列表不同就能够加以区分
// 参数列表不同指的是: 参数的类型不相同或者参数的个数不相同或者二者皆不同
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.数组:摇骰子
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.斐波那契 数列 **
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循环 (Java 5+)
// 给数组每一个找一个代言人 如循环开始 int i 来 代替 f[0] 循环一次 代替下一个 数
// 只读循环
for (int i : f) {
System.out.println(i);
}
- 4.用一个数组来存学生的成绩,算最高和最低分,平均分
String[] names = {"关羽", "张飞", "黄忠", "赵云", "马超"};
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);
作业
- 1.买彩票
红色球 1~33 蓝色球 1~16
01 05 11 17 18 22 05
08 10 22 23 31 32 11
数字不能重复
输入 几 随机选出 几组号码
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;
}