- 不死兔子: 小明今年高考,考了700,父母,给他买了一对刚刚出生小兔子,四个月后成长为成年兔子,成年后,每过一个月,假设生出一对新的小兔子
问:第n月,小明家共有多少对兔子。
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份:");
int n = sc.nextInt();
int num = rabbit(n);
System.out.println(num);
}
public static int rabbit(int n){
int num = 1;
if (n <= 4){
return 1;
}
return rabbit(n - 1) + rabbit(n - 4);
}
}
课堂练习:
1.打印
* 1
** 2
*** 3
**** 4
***** 5
public class practice1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要打印的行数:");
int n = sc.nextInt();
print(n);
}
public static void print(int n){
for (int i = 1; i <= 5; i++){
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
2.九九乘法表
/*
九九乘法表
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
*/
public class practice2 {
public static void main(String[] args) {
for (int i = 1; i <= 9;i++){
for (int j = 1; j <= i; j++){
System.out.print(j + "×" + i + "=" + (i*j) + " ");
}
System.out.println();
}
}
}
3.判断一个数是否质数
public class practice3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个数:");
int n = sc.nextInt();
boolean b = isPrime(n);
if (b){
System.out.println(n + "是质数");
}else {
System.out.println(n + "不是质数");
}
}
public static boolean isPrime(int n){
for (int i = 2; i < (n / 2) + 1; i++){
if (n % i == 0){
return false;
}
}
return true;
}
}
4.打印
*
***
*****
*******
*********
import java.util.Scanner;
public class practice4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的行数:");
int n = sc.nextInt();
print(n);
}
public static void print(int n){
for (int i = 1; i <= n; i++){
for (int j = n - i; j > 0; j--){
System.out.print(" ");
}
for (int k = 1; k <= (2 * i) - 1; k++){
System.out.print("*");
}
System.out.println();
}
}
}
练习1:
输入三边的长度,求三角形的面积和周长(海伦公式)
import java.util.Scanner;
public class practice5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一条边的长度:");
double a = sc.nextInt();
System.out.println("请输入第二条边的长度:");
double b = sc.nextInt();
System.out.println("请输入第三条边的长度:");
double c = sc.nextInt();
if (a + b > c && a + c > b && b + c > a){
double area = area(a, b, c);
System.out.println("面积为:" + area + ",周长为:" + (a + b + c));
}else {
System.out.println("这三条边无法组成三角形,请重新输入三边的长度!");
}
}
public static double area(double a, double b, double c){
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
练习2:
设计一个程序,完成(英雄)商品的购买
展示商品信息(折扣)->输入商品价格->输入购买数量->提示付款
输入付款金额->打印购买小票(扩展)
import java.util.Scanner;
public class practice6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("苹果电脑(打9折)");
System.out.println("请输入商品的价格:");
double price = sc.nextDouble();
System.out.println("请输入购买数量:");
int num = sc.nextInt();
double money = price * num * 0.9;
System.out.println("应支付:" + money +