日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA循环结构学校上机经常遇到的几题 笔记

發(fā)布時間:2024/4/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA循环结构学校上机经常遇到的几题 笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package homework.class4;import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream;public class HomeWorker {public static void main(String[] args) {// 1. 從鍵盤循環(huán)輸入正整數(shù),當(dāng)輸入-1時結(jié)束,統(tǒng)計輸入的正整數(shù)的個數(shù)。// home();// 2. 從鍵盤輸入一個整數(shù),判斷該數(shù)是否素數(shù)。素數(shù)是只能被1 和本身整除的數(shù)。可用窮舉法來判斷一個數(shù)是否是素數(shù)。// home1(); // 輸出100以內(nèi)的所有素數(shù) // home2(); // 九九乘法表 // home3(); // 分解質(zhì)因數(shù) // home4(); // 6.輸入某年某月某日,判斷這一天是這一年的第幾天? // home5(); // 7.輸入一個數(shù)值,以反向的數(shù)值方式進行輸出 // home6(); // 8.輸入一個數(shù)值,根據(jù)輸入數(shù)值打印出如下結(jié)果 // 如:輸入3 // 3 // 33 // 333 // home7(); // 9 輸入年份和月份 第六題的升級版 // 9.輸入年份和月份可以顯示出當(dāng)月一共有幾天,年份必須大于0,月份必須為1-12之間。 // 執(zhí)行情況如下 // 請輸入年份: // 當(dāng)年份輸入錯誤后會出現(xiàn)提示,并要求重新輸入年份 // 提示內(nèi)容為: // 您輸入的年份有誤!(年份必須大于0) // 請重新輸入正確的年份: // // 請輸入月份: // 當(dāng)月份輸入錯誤后會出現(xiàn)提示,并要求重新輸入月份 // 您輸入的月份有誤!(月份范圍為1-12) // 請重新輸入正確的月份: // // 所有輸入完成后會顯示當(dāng)年當(dāng)月有幾天,隨后詢問用戶是否要再次查詢? // 如果用戶選擇是,則再次執(zhí)行程序 // 如果用戶選擇否,則結(jié)束程序 home8();}private static void home8() {Scanner scan = new Scanner(System.in);while (true) { // int year,month,day;int feb = 29;int[] months = {31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // List months = Stream.of(lists).collect(Collectors.toCollection(ArrayList::new)); // List months = new ArrayList<>(Arrays.asList(lists));int currentDay;System.out.println("輸入年份(大于0):");int year = scan.nextInt();while (true) {if (0 > year) {System.out.println("您輸入的年份有誤!");System.out.println("請重新輸入正確的年份:");year = scan.nextInt();continue;}break;}// 判斷是否是閏年// GregorianCalendar:判斷年份是否是閏年的方法// 二月boolean leapYear = new GregorianCalendar().isLeapYear(year);feb = leapYear ? 29 : 28; // months.add(1,feb); // System.err.println(months.get(1));months[1] = feb; // System.err.println(months[1]); System.out.println("請輸入月份(1-12):");int month = scan.nextInt();while (true) {if (1 > month || month > 12) {System.out.println("您輸入的月份有誤!(月份范圍為1-12)");System.out.println("請重新輸入正確的月份: ");month = scan.nextInt();continue;}break;}// currentDay = (int) months.get(month-1);currentDay = months[month - 1];System.out.println("請輸入輸入日期:");int day = scan.nextInt();while (true) {if (1 > day || day > currentDay) {System.out.println("請輸入輸入日期,不大于" + currentDay + ":");day = scan.nextInt();continue;}break;}if (1 == month) {System.out.println("這一天是這一年的第" + day + "天");return;}int totalDay = 0;for (int i = 0; i < month - 1; i++) { // totalDay += (int)months.get(i);totalDay += months[i];}totalDay += day;System.out.println("這一天是這一年的第" + totalDay + "天");System.out.println("是否要再次查詢?(y/n、任意鍵退出)");String str = scan.next();if (str.equals("y")){continue;}break;}}private static void home7() {int n = 6,temp=n;for (int i = 1; i < n+1; i++){for (int j = 1;j<=i;j++){System.out.print(n);}System.out.println();}}// 例如:輸入54321 輸出12345 // 輸入879 輸出978private static void home6() {int inputVal = 54321, n;for (int i = inputVal; i > 0 ;i /=10){n = i % 10;System.out.print(n);}}private static void home5() { // String str = "2019-12-13"; // String[] split = str.split("-"); // int year = Integer.parseInt(split[0]); // int month = Integer.parseInt(split[1]); Scanner scan = new Scanner(System.in);System.out.println("輸入年份:");int year = scan.nextInt();System.out.println("請輸入月份(1-12):");int month = scan.nextInt();System.out.println("請輸入輸入日期:");int day = scan.nextInt();//判斷是否是閏年//GregorianCalendar:判斷年份是否是閏年的方法boolean leapYear = new GregorianCalendar().isLeapYear(year);// 2月int feb = leapYear?29:28; // System.out.println(feb);int[] months={31,feb,31,30,31,30,31,31,30,31,30,31};int currentDay;while(true){if (1 > month || month > 12){System.out.println("請輸入月份(1-12):");month = scan.nextInt();continue;}currentDay = months[month-1];if (1 > day || day > currentDay){System.out.println("請輸入輸入日期,不大于"+currentDay+":");day = scan.nextInt();continue;}break;}if (1 == month){System.out.println("這一天是這一年的第"+ day +"天");return;}int totalDay = 0;for (int i = 0; i < month-1; i++){totalDay += months[i];}totalDay += day;System.out.println("這一天是這一年的第"+ totalDay +"天");}private static void home4() {int n = 13;if (1 == n){System.out.println(n);return;}for (int i = 2; i <= n; i++){while (n % i == 0){if (i == n){System.out.println(n);break;}else {System.out.print(i + "*");n /= i;}}}}private static void home3() {System.out.println("1\t\t2\t\t3\t\t4\t\t5\t\t6\t\t7\t\t8\t\t9\t");System.out.println("--------------------------------------------------------------------------------");for (int i = 1; i < 10; i++){for (int j = i; j < 10;j++){ // if (i > j){ // System.out.println("\t"); // continue; // }System.out.print(i + "*" + j + "=" + i*j + "\t");}System.out.println();for (int k = 0; k < i; k++){System.out.print("\t\t");}}}/*** 判斷一個數(shù)是不是素數(shù):只能被1和本身整除* @param n* @return*/private static boolean numberIsPrime(int n) {for (int i = 2; i <= Math.sqrt(n); i++) {if (n % i == 0) {return false;}}return true;}private static void home2() {int tmp = 100;boolean flag = true;// if(tmp<1){ // System.out.print("該數(shù)不是質(zhì)數(shù)!"); // return; // } // System.out.println(Math.sqrt(tmp));if (1 == tmp){return;}for (int i = 2; i <= tmp; i++){if (numberIsPrime(i)){System.out.print(i+"\t");}}}private static void home1() {int tmp = 100;boolean flag = true;if(tmp==1){System.out.print("該數(shù)是質(zhì)數(shù)!");return;} // System.out.println(Math.sqrt(tmp));// 開根號 // for(int i = 2 ; Math.sqrt(tmp/2) >=i ; i ++ ){for(int i = 2 ; Math.sqrt(tmp) >= i ; i ++ ){if(tmp%i==0){System.out.print("該數(shù)不是質(zhì)數(shù)!");flag = false;break;}}if (false){System.out.print("該數(shù)是質(zhì)數(shù)!");}}// 1. 從鍵盤循環(huán)輸入正整數(shù),當(dāng)輸入-1時結(jié)束,統(tǒng)計輸入的正整數(shù)的個數(shù)。private static void home() {Scanner scanner = new Scanner(System.in);System.out.println("請輸入一個整數(shù):");int val = scanner.nextInt();int count = 0;while (val != -1){count++;System.out.println("請輸入一個整數(shù):");val = scanner.nextInt();}System.out.println(count);} }

轉(zhuǎn)載于:https://www.cnblogs.com/my-ordinary/p/11578973.html

總結(jié)

以上是生活随笔為你收集整理的JAVA循环结构学校上机经常遇到的几题 笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。