<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                >[danger] ##### 飛機票買票 1. 機票價格按照淡季旺季、頭等艙和經濟艙收費、輸入機票原價、月份和頭等艙或經濟艙。按照如下規則計算機票價格:旺季(5-10月)頭等艙9折,經濟艙8.5折,淡季(11月到來年4月)頭等艙7折,經濟艙6.5折 2. 將計算票價部分進行了方法抽取 ~~~ import java.util.Scanner; public class Test { public static void main(String[] args) { /* * 機票價格按照淡季旺季、頭等艙和經濟艙收費、輸入機票原價、月份和頭等艙或經濟艙。 * 按照如下規則計算機票價格:旺季(5-10月)頭等艙9折,經濟艙8.5折,淡季(11月到來年4月)頭等艙7折,經濟艙6.5折。 */ Scanner sc = new Scanner(System.in); // 輸入票價 System.out.print("請輸入票價"); double ticket = sc.nextDouble(); // 輸入購買月份 System.out.print("請輸入月份"); int month = sc.nextInt(); // 輸入購買 倉位 1頭等 2經濟 System.out.print("請輸入倉位 1頭等 2經濟"); int seat = sc.nextInt(); // 判讀月份 if (month >= 5 && month <= 10) { ticket = getTicket(ticket, seat, 0.9, 0.85); // if (seat == 1) { // ticket *= 0.9; // } else if (seat == 2) { // ticket *= 0.85; // } else { // System.out.print("沒有這個艙位"); // } } else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) { ticket = getTicket(ticket, seat, 0.7, 0.65); // if (seat == 1) { // ticket *= 0.7; // } else if (seat == 2) { // ticket *= 0.65; // } else { // System.out.print("沒有這個艙位"); // } } else { System.out.print("輸入月份不存在"); } System.out.println(ticket); } // 將計算票價部分抽取方法 public static double getTicket(double ticket, int seat, double v1, double v2) { if (seat == 1) { ticket *= v1; } else if (seat == 2) { ticket *= v2; } else { System.out.print("沒有這個艙位"); } return ticket; } } ~~~ >[danger] ##### 計算質數 1. 判斷 101 ~ 200 之間有多少個素數,并打印所有素數 ~~~ public class Test { public static void main(String[] args) { // 質數是指在大于1的自然數中,除了1和它本身以外不再有其他因數的自然數。定義 質數又稱素數 // 判斷 101 ~ 200 之間有多少個素數,并打印所有素數 int count = 0; for (int i = 101; i <= 200; i++) { boolean flag = true; for (int j = 2; j < i; j++) { if (i % j == 0) { flag = false; break; } } if (flag) { count++; } } System.out.println("一共有" + count + "個質數"); } } ~~~ >[danger] ##### 隨機驗證碼 1. 定義方法實現隨機產生一個5位的驗證碼,前四位是大寫字母或者小寫字母,最后一位是數字 ~~~ import java.util.Random; public class Test { public static void main(String[] args) { /* * 需求: * 定義方法實現隨機產生一個5位的驗證碼 * 驗證碼格式: * 長度為5 * 前四位是大寫字母或者小寫字母 * 最后一位是數字 */ // 定義一個保存大小寫字母的數組 char[] chs = new char[52]; // 利用ascii 碼一次將 字母存在字符串 for (int i = 0; i < chs.length; i++) { // 將數字轉換為char 類型 if (i < 26) { chs[i] = (char) (65 + i); } else { chs[i] = (char) (97 + i - 26); } } // 定義一個字符串類型的變量,用來記錄最終的結果 String result = ""; // 從數組中隨機取出 Random r = new Random(); for (int i = 0; i < 4; i++) { int randomIndex = r.nextInt(chs.length); // 利用隨機索引,獲取對應的元素 // System.out.println(chs[randomIndex]); result = result + chs[randomIndex]; } // 3.隨機抽取一個數字0~9 int number = r.nextInt(10); // 生成最終的結果 result = result + number; // 打印最終結果 System.out.println(result); } } ~~~ >[danger] ##### 評委打分 1. 在唱歌比賽中,有6名評委給選手打分,分數范圍是\[0 - 100\]之間的整數。選手的最后得分為:去掉最高分、最低分后的4個評委的平均分,請完成上述過程并計算出選手的得分。 ~~~ import java.util.Scanner; public class Test { public static void main(String[] args) { int[] sources = getSources(4); int max = getMaxSource(sources); int min = getMinSource(sources); int sumSource = sum(sources); double avg = (sumSource - max - min) / (sources.length - 2); System.out.print(avg); } // 分數錄入 public static int[] getSources(int s) { Scanner sc = new Scanner(System.in); int[] sources = new int[4]; for (int i = 0; i < s;) { System.out.print(i + 1 + "號評委打分"); int source = sc.nextInt(); if (source <= 100) { sources[i] = source; i++; } else { System.out.println("成績超出了范圍,繼續錄入,當前的i為:" + i + 1); } } return sources; } // 數組求和 public static int sum(int[] sources) { int sumSource = 0; for (int i = 0; i < sources.length; i++) { sumSource += sources[i]; } return sumSource; } // 獲取數組中的最小值 public static int getMinSource(int[] sources) { int min = sources[0]; for (int i = 1; i < sources.length; i++) { if (min > sources[i]) { min = sources[i]; } } return min; } // 獲取數組中的最大值 public static int getMaxSource(int[] sources) { int max = sources[0]; for (int i = 1; i < sources.length; i++) { if (max < sources[i]) { max = sources[i]; } } return max; } } ~~~ >[danger] ##### 轉換加密 ~~~ import java.util.Arrays; public class Test { public static void main(String[] args) { /* * 某系統的數字密碼(大于0)。比如1983,采用加密方式進行傳輸, * 規則如下: * 每位數加上5 * 再對10求余, * 最后將所有數字反轉, * 得到一串新數。 */ int[] arr = { 1, 9, 8, 3 }; // 每一位加五然后對10求余 for (int i = 0; i < arr.length; i++) { int temp = arr[i]; temp += 5; temp %= 10; arr[i] = temp; } // 將數組前后交換定義兩個指針 for (int i = 0, j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } System.out.print(Arrays.toString(arr)); // 8346 } } ~~~ >[danger] ##### 將加密密碼反向推到 ~~~ package com.itheima.test; public class Test8 { public static void main(String[] args) { /* * 某系統的數字密碼(大于0)。比如1983,采用加密方式進行傳輸, * 規則如下: * 每位數加上5 * 再對10求余, * 最后將所有數字反轉, * 得到一串新數。 * 按照以上規則進行解密: * 比如1983加密之后變成8346,解密之后變成1983 */ // 1.定義數組記錄解密之后的結果 int[] arr = { 8, 3, 4, 6 }; // 2.反轉 for (int i = 0, j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // 3.由于加密是通過對10取余的方式進行獲取的 // 所以在解密的時候就需要判斷因為出現數字都是在0-9之間 // 加5后范圍是 5 -14 直接,相對 14為例 如果加密后是4 則原本為14 // 0~4之間+10 5~9數字不變 for (int i = 0; i < arr.length; i++) { if (arr[i] >= 0 && arr[i] <= 4) { arr[i] = arr[i] + 10; } } // 4.每一位減5 for (int i = 0; i < arr.length; i++) { arr[i] = arr[i] - 5; } // 5.獲取數組里面的每一位數字拼接成最終的結果 int number = 0; for (int i = 0; i < arr.length; i++) { number = number * 10 + arr[i]; } System.out.println(number); } } ~~~ >[danger] ##### 將數字每一位存在數組 1. 給定一個數字 1234 變成數組形式`[1,2,3,4]` ~~~ import java.util.Arrays; public class Test { public static void main(String[] args) { int num = 1234; // 臨時保存 int temp = num; // 計算出當前數字位數 int count = 0; while (num != 0) { num /= 10; count++; } int[] nums = new int[count]; int index = nums.length - 1; while (temp != 0) { int ge = temp % 10; temp /= 10; nums[index--] = ge; } // 驗證結果 1 2 3 4 for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " "); } } } ~~~ >[danger] ##### 打亂數組位置 * 方式一 ~~~ import java.util.Random; public class Test9 { public static void main(String[] args) { /* 需求: 一個大V直播抽獎,獎品是現金紅包,分別有{2, 588 , 888, 1000, 10000}五個獎金。 請使用代碼模擬抽獎,打印出每個獎項,獎項的出現順序要隨機且不重復。 打印效果如下:(隨機順序,不一定是下面的順序) 888元的獎金被抽出 588元的獎金被抽出 10000元的獎金被抽出 1000元的獎金被抽出 2元的獎金被抽出 */ //分析: //1.定義數組表示獎池 int[] arr = {2, 588, 888, 1000, 10000}; //2.定義新數組用于存儲抽獎的結果 int[] newArr = new int[arr.length]; //3.抽獎 Random r = new Random(); //因為有5個獎項,所以這里要循環5次 for (int i = 0; i < 5; ) { //獲取隨機索引 int randomIndex = r.nextInt(arr.length); //獲取獎項 int prize = arr[randomIndex]; //判斷當前的獎項是否存在,如果存在則重新抽取,如果不存在,就表示是有效獎項 boolean flag = contains(newArr, prize); if(!flag){ //把當前抽取到的獎項添加到newArr當中 newArr[i] = prize; //添加完畢之后,移動索引 i++; } } //4.遍歷newArr for (int i = 0; i < newArr.length; i++) { System.out.println(newArr[i]); } } //判斷prize在數組當中是否存在 //存在:true //不存在:false public static boolean contains(int[] arr,int prize){ for (int i = 0; i < arr.length; i++) { if(arr[i] == prize){ return true; } } return false; } } ~~~ * 方式二 ~~~ import java.util.Random; public class Test10 { public static void main(String[] args) { /* 需求: 一個大V直播抽獎,獎品是現金紅包,分別有{2, 588 , 888, 1000, 10000}五個獎金。 請使用代碼模擬抽獎,打印出每個獎項,獎項的出現順序要隨機且不重復。 打印效果如下:(隨機順序,不一定是下面的順序) 888元的獎金被抽出 588元的獎金被抽出 10000元的獎金被抽出 1000元的獎金被抽出 2元的獎金被抽出 */ //1.把獎池里面的所有獎項打亂順序 int[] arr = {2, 588, 888, 1000, 10000}; Random r = new Random(); for (int i = 0; i < arr.length; i++) { //獲取隨機索引 int randomIndex = r.nextInt(arr.length); //拿著i跟隨機索引randomIndex上的值進行交換 int temp = arr[i]; arr[i] = arr[randomIndex]; arr[randomIndex] = temp; } //2.遍歷獎池,從0索引開始獲取每一個獎項 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } } ~~~ >[danger] ##### 雙色球隨機數 ~~~ import java.util.Random; import java.util.Scanner; public class Test11 { public static void main(String[] args) { //1.生成中獎號碼 int[] arr = createNumber(); // 123456 7 System.out.println("======================="); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("======================="); //2.用戶輸入彩票號碼(紅球 + 藍球)//654321 int[] userInputArr = userInputNumber(); //3.判斷用戶的中獎情況 //紅球 藍球 int redCount = 0; int blueCount = 0; //判斷紅球 for (int i = 0; i < userInputArr.length - 1; i++) { int redNumber = userInputArr[i]; for (int j = 0; j < arr.length - 1; j++) { if(redNumber == arr[j]){ redCount++; //如果找到了,那么后面的數字就沒有必要繼續比較了 //跳出內循環,繼續判斷下一個紅球號碼是否中獎 break; } } } //判斷藍球 int blueNumber = userInputArr[userInputArr.length-1]; if(blueNumber == arr[arr.length - 1]){ blueCount++; } //根據紅球的個數以及藍球的個數來判斷中獎情況 if(redCount == 6 && blueCount == 1){ System.out.println("恭喜你,中獎1000萬"); }else if(redCount == 6 && blueCount == 0){ System.out.println("恭喜你,中獎500萬"); }else if(redCount == 5 && blueCount == 1){ System.out.println("恭喜你,中獎3000"); }else if((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)){ System.out.println("恭喜你,中獎200"); }else if((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)){ System.out.println("恭喜你,中獎10"); }else if((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1)|| (redCount == 0 && blueCount == 1)){ System.out.println("恭喜你,中獎5"); }else{ System.out.println("謝謝參與,謝謝惠顧"); } } public static int[] userInputNumber() { //1.創建數組用于添加用戶購買的彩票號碼 //6個紅球 1個藍球 數組長度:7 int[] arr = new int[7]; //2.利用鍵盤錄入讓用輸入 Scanner sc = new Scanner(System.in); //讓用戶輸入紅球號碼 for (int i = 0; i < 6; ) { System.out.println("請輸入第" + (i + 1) + "個紅球號碼"); int redNumber = sc.nextInt(); //redNumber 在1~33 唯一不重復 if (redNumber >= 1 && redNumber <= 33) { boolean flag = contains(arr, redNumber); if (!flag) { //不存在 //有效的,可以添加到數組當中 arr[i] = redNumber; i++; } else { //存在 System.out.println("當前紅球號碼已經存在,請重新輸入"); } } else { System.out.println("當前紅球號碼超出范圍"); } } //讓用戶輸入籃球號碼 System.out.println("請輸入籃球號碼"); //1~16 while (true) { int blueNumber = sc.nextInt(); if (blueNumber >= 1 && blueNumber <= 16) { arr[arr.length - 1] = blueNumber; break; } else { System.out.println("當前籃球號碼超出范圍"); } } return arr; } public static int[] createNumber() { //1.創建數組用于添加中獎號碼 //6個紅球 1個藍球 數組長度:7 int[] arr = new int[7]; //2.隨機生成號碼并添加到數組當中 //紅球:不能重復的 1 2 3 4 5 6 //藍球:可以跟紅球號碼重復 5 //生成紅球號碼并添加到數組當中 Random r = new Random(); for (int i = 0; i < 6; ) { //獲取紅球號碼 int redNumber = r.nextInt(33) + 1; boolean flag = contains(arr, redNumber); if (!flag) { //把紅球號碼添加到數組當中 arr[i] = redNumber; i++; } } //生成藍球號碼并添加到數組當中 int blueNumber = r.nextInt(16) + 1; arr[arr.length - 1] = blueNumber; return arr; } //用于判斷數組在數組中是否存在 public static boolean contains(int[] arr, int number) { for (int i = 0; i < arr.length; i++) { if (arr[i] == number) { return true; } } return false; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看