本篇內(nèi)容主要講解“怎么編寫Java程序使用switch結(jié)構(gòu)計算對應(yīng)月份的天數(shù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么編寫Java程序使用switch結(jié)構(gòu)計算對應(yīng)月份的天數(shù)”吧!
在海州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,營銷型網(wǎng)站,外貿(mào)網(wǎng)站制作,海州網(wǎng)站建設(shè)費用合理。
有題如下:
編寫 Java 程序,輸入年份和月份,使用 switch 結(jié)構(gòu)計算對應(yīng)月份的天數(shù)。
月份為 1、3、5、7、8、10、12 時,天數(shù)為 31 天。
月份為 4、6、9、11 時,天數(shù)為 30 天。
月份為 2 時,若為閏年,天數(shù)為 29 天,否則,天數(shù)為 28 天。
實現(xiàn)如下程序:
package rjxy2019_java_demo;import java.util.Scanner;public class SwitchWithDays { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter a year:");int year = input.nextInt(); System.out.println("Please enter a month:");int month = input.nextInt();int day = 0;boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));switch(month) { case 1:case 3:case 5:case 7:case 8:case 10:case 12:day = 31;break;case 4:case 6:case 9:case 11:day = 30;break;case 2:if(isLeapYear == true) day = 29;else day = 28;break;default:System.out.println("Error:invalid input"); System.exit(1);} System.out.println(year + "年" + month + "月一共" + day + "天");}}
驗證,當輸入為 2009 年 2 月時:
package rjxy2019_java_demo;import java.util.Scanner;public class IfElseWithDays { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter a year:");int year = input.nextInt(); System.out.println("Please enter a month:");int month = input.nextInt();int day = 0;boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12) day = 31;else{ if(month == 4 || month == 6 || month == 9 || month == 11) day = 30;else { if(month == 2) { if(isLeapYear == true) day = 29;else day = 28;}else { System.out.println("Error:invalid input"); System.exit(1);}}} System.out.println(year + "年" + month + "月一共" + day + "天");}}
到此,相信大家對“怎么編寫Java程序使用switch結(jié)構(gòu)計算對應(yīng)月份的天數(shù)”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!
新聞名稱:怎么編寫Java程序使用switch結(jié)構(gòu)計算對應(yīng)月份的天數(shù)
文章URL:http://www.jinyejixie.com/article32/jogesc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站改版、網(wǎng)站內(nèi)鏈、外貿(mào)建站、品牌網(wǎng)站設(shè)計、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)