聯(lián)系

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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    葉總韓閱讀 5,227評(píng)論 0 41
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,082評(píng)論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,740評(píng)論 18 399
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile麗語閱讀 4,105評(píng)論 0 6
  • 豆瓣APP的上線已經(jīng)有很長(zhǎng)周期,版本更迭數(shù)次,在功能和交互界面上都有了很大改變。這次通過倒推的形式制作PRD文檔,...
    郅絕閱讀 3,422評(píng)論 0 12

友情鏈接更多精彩內(nèi)容