commit 81a1888519d26951e8dfdc2223e009a0246c7c23 Author: zyx <1029606625@qq.com> Date: Fri Sep 9 15:52:46 2022 +0800 2022/09/09 第一次提交 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..3d9eb8b --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Demo1.java \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c1b38a2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Week2/Week2.iml b/Week2/Week2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week2/Week2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week2/src/Demo01.java b/Week2/src/Demo01.java new file mode 100644 index 0000000..7a1d84e --- /dev/null +++ b/Week2/src/Demo01.java @@ -0,0 +1,43 @@ +import java.util.Scanner; + +public class Demo01 { + public static void main(String[] args) { + String again; + do { + int randomNumber = 1 + (int) (Math.random() * 1000); +// System.out.println(randomNumber); + System.out.println("I have a number between 1 and 1000. \nCan you guess my number?\nPlease type your first guess."); + Scanner scanner = new Scanner(System.in); + + boolean success = false; + int cnt = 1; + for (int i = 1; i <= 10; i++) { + int guessNumber = scanner.nextInt(); + + if (randomNumber == guessNumber) { + System.out.println("Excellent! You guessed the number"); + success = true; + break; + } else { + if (i == 10) break; + if (guessNumber > randomNumber) { + System.out.println("Too High. Try Again."); + } else { + System.out.println("Too Low. Try Again"); + } + } + cnt++; + System.out.println("Please type your guess again:"); + } + if (success) { + System.out.println("Success! You have cost " + cnt + " times"); + } else { + System.out.println("Sorry! You failed!\nThe answer is: " + randomNumber); + } + + System.out.println("Would you like to play again (y or n)?:"); + again = scanner.next(); + }while ("y".equalsIgnoreCase(again)); + System.out.println("Game Over! Welcome to play again!"); + } +} diff --git a/Week2/src/work/Work1.java b/Week2/src/work/Work1.java new file mode 100644 index 0000000..6a19ccb --- /dev/null +++ b/Week2/src/work/Work1.java @@ -0,0 +1,49 @@ +package work; + +import java.util.Scanner; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/7 - 9:23 + */ +public class Work1 { + public static void main(String[] args) { + String again; + do { + int randomNumber = 1 + (int) (Math.random() * 1000); +// System.out.println(randomNumber); + System.out.println("I have a number between 1 and 1000. \nCan you guess my number?\nPlease type your first guess."); + Scanner scanner = new Scanner(System.in); + + boolean success = false; + int cnt = 1; + for (int i = 1; i <= 10; i++) { + int guessNumber = scanner.nextInt(); + + if (randomNumber == guessNumber) { + System.out.println("Excellent! You guessed the number"); + success = true; + break; + } else { + if (i == 10) break; + if (guessNumber > randomNumber) { + System.out.println("Too High. Try Again."); + } else { + System.out.println("Too Low. Try Again"); + } + } + cnt++; + System.out.println("Please type your guess again:"); + } + if (success) { + System.out.println("Success! You have cost " + cnt + " times"); + } else { + System.out.println("Sorry! You failed!\nThe answer is: " + randomNumber); + } + + System.out.println("Would you like to play again (y or n)?:"); + again = scanner.next(); + } while ("y".equalsIgnoreCase(again)); + System.out.println("Game Over! Welcome to play again!"); + } +} diff --git a/Week2/src/work/Work2.java b/Week2/src/work/Work2.java new file mode 100644 index 0000000..ec99a58 --- /dev/null +++ b/Week2/src/work/Work2.java @@ -0,0 +1,43 @@ +package work; + +import java.util.Scanner; + +/** + * @author : 张宇轩 + * @createTime : 2022/9/7 - 9:23 + */ +public class Work2 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = -1; + while (n == -1) { + System.out.println("请输入一个正整数(3<=n<=7):"); + n = checkInputNumber(scanner,3,7); + } + + int begin = (int) Math.pow(10,n-1), end = (int) (Math.pow(10,n) -1); + for (int i = begin; i <= end; i++) { + int sum = 0, tmp = i; + while (tmp != 0){ + sum += (int) Math.pow(tmp%10,n); + tmp /= 10; + } + if(sum == i) System.out.println(sum); + } + } + + public static int checkInputNumber(Scanner scanner, int low, int high) { + String input = scanner.next(); + try { + int opt = Integer.parseInt(input); + if (opt >= low && opt <= high) + return opt; + else { + System.out.println("输入无效,只能输入" + low + "-" + high); + } + } catch (NumberFormatException e) { + System.out.println("输入无效,只能输入" + low + "-" + high); + } + return -1; + } +} diff --git a/Week2/src/work/Work2_0908.java b/Week2/src/work/Work2_0908.java new file mode 100644 index 0000000..028b7b9 --- /dev/null +++ b/Week2/src/work/Work2_0908.java @@ -0,0 +1,184 @@ +package work; + +import java.io.IOException; +import java.util.Scanner; + +/** + * 菜单 + * 作业2 + * 1.猜数 + * 2.求解水仙花数 + * 3.打印图形 + * 4.求解表达式 + */ +public class Work2_0908 { + public static void main(String[] args) throws IOException { + Scanner scanner = new Scanner(System.in); + while (true){ + System.out.println("作业2"); + System.out.println("1.猜数游戏"); + System.out.println("2.求解水仙花数"); + System.out.println("3.打印菱形"); + System.out.println("4.求解表达式"); + System.out.println("0.退出程序"); + int opt = -1; + while (opt == -1) { + System.out.print(">>>请输入选项(0-4):"); + opt = checkInputNumber(scanner,0,4); + } + switch (opt){ + case 1: + guessNumber(scanner); + break; + case 2: + getNarcissisticNumber(scanner); + break; + case 3: + printRhombus(); + break; + case 4: + calcExpression(scanner); + break; + default: + return; + } + System.out.println("按任意键继续..."); + System.in.read(); + } + + } + + /** + * 猜数游戏 + * @param scanner 通用的输入 + */ + public static void guessNumber(Scanner scanner) { + String again; + do { + int randomNumber = 1 + (int) (Math.random() * 1000); + System.out.println("I have a number between 1 and 1000. \nCan you guess my number?\nPlease type your first guess."); + + boolean success = false; + int cnt = 1; + for (int i = 1; i <= 10; i++) { + int guessNumber = scanner.nextInt(); + + if (randomNumber == guessNumber) { + System.out.println("Excellent! You guessed the number"); + success = true; + break; + } else { + if (i == 10) break; + if (guessNumber > randomNumber) { + System.out.println("Too High. Try Again."); + } else { + System.out.println("Too Low. Try Again"); + } + } + cnt++; + System.out.println("Please type your guess again:"); + } + if (success) { + System.out.println("Success! You have cost " + cnt + " times"); + } else { + System.out.println("Sorry! You failed!\nThe answer is: " + randomNumber); + } + + System.out.println("Would you like to play again (y or n)?:"); + again = scanner.next(); + } while ("y".equalsIgnoreCase(again)); + System.out.println("Game Over! Welcome to play again!"); + } + + /** + * 求解水仙花数 + * @param scanner 通用的输入 + */ + public static void getNarcissisticNumber(Scanner scanner) { + int n = -1; + while (n == -1) { + System.out.println("请输入一个正整数(3<=n<=7):"); + n = checkInputNumber(scanner, 3, 7); + } + + int begin = (int) Math.pow(10, n - 1), end = (int) (Math.pow(10, n) - 1); + for (int i = begin; i <= end; i++) { + int sum = 0, tmp = i; + while (tmp != 0) { + sum += (int) Math.pow(tmp % 10, n); + tmp /= 10; + } + if (sum == i) System.out.println(sum); + } + System.out.println("求解完毕!"); + } + + + /** + * 输出菱形图案 + */ + public static void printRhombus() { + for (int i = 1; i <= 3; i++) {//上半部分 + for (int j = 1; j <= 3 - i; j++) { + System.out.print(" "); + } + for (int j = 1; j <= 2 * i - 1; j++) { + System.out.print("*"); + } + System.out.println(); + } + for (int i = 1; i <= 3; i++) {//下半部分 + for (int j = 1; j <= i; j++) { + System.out.print(" "); + } + for (int j = 1; j <= 5 - 2 * i; j++) { + System.out.print("*"); + } + System.out.println(); + } + } + + /** + * 计算表达式 + * @param scanner 通用的输入 + */ + public static void calcExpression(Scanner scanner) { + System.out.println("计算1 + 1/2 - 1/3 + 1/4 - 1/5 + ... + 1/n"); + + int n; + while (true) { + System.out.println("请输入n(n>=1):"); + String input = scanner.next(); + try { + n = Integer.parseInt(input); + if (n >= 1) break; + } catch (NumberFormatException ignored) {} + } + + int flag = 1; + double sum = 1; + if (n >= 2) { + for (int i = 2; i <= n; i++) { + sum += 1.0 * flag / i; + flag = -flag; + } + } + System.out.println("表达式的值为:" + sum); + } + + public static int checkInputNumber(Scanner scanner, int low, int high) { + String input = scanner.next(); + try { + int opt = Integer.parseInt(input); + if (opt >= low && opt <= high) + return opt; + else { + System.out.println("输入无效,只能输入" + low + "-" + high); + } + } catch (NumberFormatException e) { + System.out.println("输入无效,只能输入" + low + "-" + high); + } + return -1; + } +} + diff --git a/java_class.iml b/java_class.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/java_class.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/week1/src/Demo1.java b/week1/src/Demo1.java new file mode 100644 index 0000000..564f91c --- /dev/null +++ b/week1/src/Demo1.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class Demo1 { + public static void main(String[] args) { + final double managerCommission = 0.12; + final double siteCommission = 0.03; + + String songTitle; + double songPrice; + int copies; + double totalRevenue; + double mamagerRevenue; + double siteRevenue; + + Scanner scanner = new Scanner(System.in); + System.out.println(); + songTitle = scanner.nextLine(); + songPrice = scanner.nextDouble(); + copies = scanner.nextInt(); + + totalRevenue = songPrice * copies; + mamagerRevenue = totalRevenue * managerCommission; + siteRevenue = totalRevenue * siteCommission; + + System.out.println(totalRevenue); + System.out.println(mamagerRevenue); + System.out.println(siteRevenue); + } +} diff --git a/week1/src/Demo2.java b/week1/src/Demo2.java new file mode 100644 index 0000000..101373d --- /dev/null +++ b/week1/src/Demo2.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class Demo2 { + public static void main(String[] args) { + final int BOX_CNT = 24; + final int CONTAINER_CNT = 75; + + System.out.println("请输入饼干总数:"); + Scanner scanner = new Scanner(System.in); + int totalBiscuit = scanner.nextInt(); + + int remainBiscuit = totalBiscuit % BOX_CNT; + int boxNumber = totalBiscuit / BOX_CNT; + int remainBox = boxNumber % CONTAINER_CNT; + int containerNumber = boxNumber / CONTAINER_CNT; + + System.out.println(totalBiscuit + "个饼干装了" + boxNumber + "个盒子," + boxNumber + "个盒子装了" + containerNumber + "个容器"); + System.out.println("剩余" + remainBiscuit + "个饼干," + remainBox + "个盒子"); + } +} diff --git a/week1/src/work/Work1.java b/week1/src/work/Work1.java new file mode 100644 index 0000000..4ebae3d --- /dev/null +++ b/week1/src/work/Work1.java @@ -0,0 +1,25 @@ +package work; + +import java.util.Scanner; + +public class Work1 { + public static void main(String[] args) { + System.out.println("请输入价格:"); + double price = new Scanner(System.in).nextDouble(); + int twoDollarCnt = (int) price / 2; + int oneDollarCnt = (int) price % 2; + + double decimal = (price*100) - twoDollarCnt*200 - oneDollarCnt*100; + int fiftyCentCnt = (int) (decimal / 50); + int twentyCentCnt = (int) ((decimal - 50*fiftyCentCnt) / 20); + int tenCentCnt = (int) ((decimal - 50*fiftyCentCnt - 20*twentyCentCnt) / 10); + int fiveCentCnt = (int) ((decimal - 50*fiftyCentCnt - 20*twentyCentCnt - 10*tenCentCnt) / 5); + + System.out.println("$2数量:" + twoDollarCnt); + System.out.println("$1数量:" + oneDollarCnt); + System.out.println("¢50数量:" + fiftyCentCnt); + System.out.println("¢20数量:" + twentyCentCnt); + System.out.println("¢10数量:" + tenCentCnt); + System.out.println("¢5数量:" + fiveCentCnt); + } +} diff --git a/week1/src/work/Work2.java b/week1/src/work/Work2.java new file mode 100644 index 0000000..59dd120 --- /dev/null +++ b/week1/src/work/Work2.java @@ -0,0 +1,67 @@ +package work; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Scanner; + +public class Work2 { + public static void main(String[] args) { + double price = 0; + Scanner scanner = new Scanner(System.in); + System.out.println("输入图片长度(英寸):"); + double length = scanner.nextDouble(); + System.out.println("输入图片宽度(英寸):"); + double width = scanner.nextDouble(); + + //纸板和玻璃的面积按与 照片 等大处理 + price += length * width * 0.09;//照片 + + double framePerimeter = 2 * length + 2 * width + 4;//框架周长 + + System.out.println("选择画框类型(0:普通 / 1:花式):"); + int optFrame = -1; + while (optFrame == -1){ + optFrame = checkInputNumber(scanner,0,1); + } + if (optFrame == 0){ + price += framePerimeter * 0.15; + }else price += framePerimeter * 0.25; + + System.out.println("选择画框颜色(0:白色 / 1:其他):"); + int optColor = -1; + while (optColor == -1){ + optColor = checkInputNumber(scanner,0,1); + } + if (optColor == 1) { + price += framePerimeter * 0.1; + } + + System.out.println("是否需要放置皇冠(0:是 / 1:否):"); + int optIfCrown = -1; + while (optIfCrown == -1){ + optIfCrown = checkInputNumber(scanner,0,1); + } + if (optIfCrown == 0){ + System.out.println("输入要放置的皇冠数量:"); + int crownCnt = scanner.nextInt(); + price += crownCnt * 0.35; + } + System.out.println("总费用:" + new BigDecimal(price).setScale(2, RoundingMode.HALF_UP)); + } + + + public static int checkInputNumber(Scanner scanner, int low, int high) { + String input = scanner.next(); + try { + int opt = Integer.parseInt(input); + if (opt >= low && opt <= high) + return opt; + else { + System.out.println("输入无效,只能输入" + low + "-" + high); + } + } catch (NumberFormatException e) { + System.out.println("输入无效,只能输入" + low + "-" + high); + } + return -1; + } +} diff --git a/week1/week1.iml b/week1/week1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/week1/week1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file