package com.itheima;
import java.util.Scanner;
/*
* 需求:鍵盤錄入一個(gè)月份,輸出該月份對(duì)應(yīng)的季節(jié)。
* 一年有四季
* 3,4,5 春季
* 6,7,8 夏季
* 9,10,11 秋季
* 12,1,2 冬季
*
* 分析:
* A:鍵盤錄入一個(gè)月份,用Scanner實(shí)現(xiàn)
* B:判斷該月份是幾月,根據(jù)月份輸出對(duì)應(yīng)的季節(jié)
* if
* switch
*/
public class Test {
public static void main(String[] args) {
//鍵盤錄入一個(gè)月份,用Scanner實(shí)現(xiàn)
Scanner sc = new Scanner(System.in);
//接收數(shù)據(jù)
System.out.println("請(qǐng)輸入月份(1-12):");
int month = sc.nextInt();
/*
if(month>12 || month<1) {
System.out.println("你輸入的月份有誤");
}else if(month == 1) {
System.out.println("冬季");
}else if(month == 2) {
System.out.println("冬季");
}else if(month == 3) {
System.out.println("春季");
}else if(month == 4) {
System.out.println("春季");
}else if(month == 5) {
System.out.println("春季");
}else if(month == 6) {
System.out.println("夏季");
}else if(month == 7) {
System.out.println("夏季");
}else if(month == 8) {
System.out.println("夏季");
}else if(month == 9) {
System.out.println("秋季");
}else if(month == 10) {
System.out.println("秋季");
}else if(month == 11) {
System.out.println("秋季");
}else {
System.out.println("冬季");
}
*/
//目前我們已經(jīng)實(shí)現(xiàn)了我們的需求
//但是我覺得代碼稍微有些麻煩
//所以我想改進(jìn)
//如何改進(jìn)呢?能不能把相同季節(jié)的月份放到一起判斷呢
//能
//month == 3 || month == 4 || month == 5
if((month == 1) || (month == 2) || (month == 12)) {
System.out.println("冬季");
}else if((month == 3) || (month == 4) || (month == 5)) {
System.out.println("春季");
}else if((month == 6) || (month == 7) || (month == 8)) {
System.out.println("夏季");
}else if((month == 9) || (month == 10) || (month == 11)) {
System.out.println("秋季");
}else {
System.out.println("你輸入的月份有誤");
}
}
}
switch(month) {
case 1:
case 2:
case 12:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("你輸入的月份有誤");
break;
}
}
}
package com.itheima;
/*
* 需求:打印5位數(shù)中的所有回文數(shù)。
* 什么是回文數(shù)呢?舉例:12321是回文數(shù),個(gè)位與萬位相同,十位與千位相同。
*
* 分析:
* A:5位數(shù)告訴了我們數(shù)據(jù)的范圍,用for循環(huán)實(shí)現(xiàn)
* B:拿到每一個(gè)5位數(shù)后,獲取其個(gè)位,十位,千位,萬位的數(shù)據(jù)
* 如何獲取呢?假設(shè)x是一個(gè)5位數(shù)
* 個(gè)位:x%10
* 十位:x/10%10
* 千位:x/10/10/10%10
* 萬位:x/10/10/10/10%10
* C:根據(jù)條件進(jìn)行判斷,把滿足條件的數(shù)據(jù)輸出即可
*/
public class Test3 {
public static void main(String[] args) {
//5位數(shù)告訴了我們數(shù)據(jù)的范圍,用for循環(huán)實(shí)現(xiàn)
for(int x=10000; x<100000; x++) {
//拿到每一個(gè)5位數(shù)后,獲取其個(gè)位,十位,千位,萬位的數(shù)據(jù)
int ge = x%10;
int shi = x/10%10;
int qian = x/10/10/10%10;
int wan = x/10/10/10/10%10;
//根據(jù)條件進(jìn)行判斷,把滿足條件的數(shù)據(jù)輸出即可
if((ge == wan) && (shi == qian)) {
System.out.println(x);
}
}
}
}
* 需求:
* 有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔子,
* 假如兔子都不死,問第二十個(gè)月的兔子對(duì)數(shù)為多少?
*
* 規(guī)律:
* 第一個(gè)月:1
* 第二個(gè)月:1
* 第三個(gè)月:2
* 第四個(gè)月:3
* 第五個(gè)月:5
* ...
*
* 規(guī)律:從第三個(gè)月開始,每個(gè)月的兔子對(duì)數(shù)是前兩個(gè)月的兔子對(duì)數(shù)之和
*? ? ? ? ? 第一個(gè)月和第二個(gè)月的兔子對(duì)數(shù)都是1
*
* 分析:
* A:由于數(shù)據(jù)比較多,所以我們就定義數(shù)組來實(shí)現(xiàn)了
* int[] arr = new int[20];
* B:給數(shù)組的元素賦值
* arr[0] = 1
* arr[1] = 2
* C:找規(guī)律
* arr[2] = arr[1] + arr[0];
* arr[3] = arr[2] + arr[1];
* arr[4] = arr[3] + arr[2];
* arr[5] = arr[4] + arr[3];
* ...
*/
public class Test4 {
public static void main(String[] args) {
//定義數(shù)組
int[] arr = new int[20];
//給數(shù)組的元素賦值
arr[0] = 1;
arr[1] = 1;
//找規(guī)律賦值
for(int x=2; x<arr.length; x++){}
arr[x]=arr[x-2]+arr[x-1]
syso