JAVA循环结构学校上机经常遇到的几题 笔记
生活随笔
收集整理的這篇文章主要介紹了
JAVA循环结构学校上机经常遇到的几题 笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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. 從鍵盤循環輸入正整數,當輸入-1時結束,統計輸入的正整數的個數。// home();// 2. 從鍵盤輸入一個整數,判斷該數是否素數。素數是只能被1 和本身整除的數。可用窮舉法來判斷一個數是否是素數。// home1();
// 輸出100以內的所有素數
// home2();
// 九九乘法表
// home3();
// 分解質因數
// home4();
// 6.輸入某年某月某日,判斷這一天是這一年的第幾天?
// home5();
// 7.輸入一個數值,以反向的數值方式進行輸出
// home6();
// 8.輸入一個數值,根據輸入數值打印出如下結果
// 如:輸入3
// 3
// 33
// 333
// home7();
// 9 輸入年份和月份 第六題的升級版
// 9.輸入年份和月份可以顯示出當月一共有幾天,年份必須大于0,月份必須為1-12之間。
// 執行情況如下
// 請輸入年份:
// 當年份輸入錯誤后會出現提示,并要求重新輸入年份
// 提示內容為:
// 您輸入的年份有誤!(年份必須大于0)
// 請重新輸入正確的年份:
//
// 請輸入月份:
// 當月份輸入錯誤后會出現提示,并要求重新輸入月份
// 您輸入的月份有誤!(月份范圍為1-12)
// 請重新輸入正確的月份:
//
// 所有輸入完成后會顯示當年當月有幾天,隨后詢問用戶是否要再次查詢?
// 如果用戶選擇是,則再次執行程序
// 如果用戶選擇否,則結束程序
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");}}}/*** 判斷一個數是不是素數:只能被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("該數不是質數!");
// 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("該數是質數!");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("該數不是質數!");flag = false;break;}}if (false){System.out.print("該數是質數!");}}// 1. 從鍵盤循環輸入正整數,當輸入-1時結束,統計輸入的正整數的個數。private static void home() {Scanner scanner = new Scanner(System.in);System.out.println("請輸入一個整數:");int val = scanner.nextInt();int count = 0;while (val != -1){count++;System.out.println("請輸入一個整數:");val = scanner.nextInt();}System.out.println(count);}
}
轉載于:https://www.cnblogs.com/my-ordinary/p/11578973.html
總結
以上是生活随笔為你收集整理的JAVA循环结构学校上机经常遇到的几题 笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle学习(十六)Oracle安装
- 下一篇: 函数进阶,仅作了解