diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 3d9eb8b..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -Demo1.java \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index c1b38a2..3aa439a 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Week3/Week3.iml b/Week3/Week3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week3/Week3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week3/src/pta/Main.java b/Week3/src/pta/Main.java new file mode 100644 index 0000000..cd58d9d --- /dev/null +++ b/Week3/src/pta/Main.java @@ -0,0 +1,25 @@ +package pta; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + + int[] pow = new int[10]; + for (int i = 0; i < 10; i++) { + pow[i] = (int) Math.pow(i,n); + } + + 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 += pow[tmp%10]; + tmp /= 10; + } + if (sum == i) System.out.println(sum); + } + } +} \ No newline at end of file diff --git a/Week3/src/work/Exercise1.java b/Week3/src/work/Exercise1.java new file mode 100644 index 0000000..5f3872d --- /dev/null +++ b/Week3/src/work/Exercise1.java @@ -0,0 +1,44 @@ +package work; + +import java.util.Scanner; + +public class Exercise1 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + 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!"); + } +} diff --git a/Week3/src/work/Exercise2.java b/Week3/src/work/Exercise2.java new file mode 100644 index 0000000..df76711 --- /dev/null +++ b/Week3/src/work/Exercise2.java @@ -0,0 +1,46 @@ +package work; + +import java.util.Scanner; + +public class Exercise2 { + 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[] pow = new int[10]; + for (int i = 0; i < 10; i++) { + pow[i] = (int) Math.pow(i, n); + } + + 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 += pow[tmp % 10]; + tmp /= 10; + } + if (sum == i) System.out.println(sum); + } + System.out.println("求解完毕!"); + } + + 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/Week3/src/work/Exercise3.java b/Week3/src/work/Exercise3.java new file mode 100644 index 0000000..ee08656 --- /dev/null +++ b/Week3/src/work/Exercise3.java @@ -0,0 +1,129 @@ +package work; + +import java.util.Arrays; +import java.util.Scanner; + +public class Exercise3 { + static int[] score = null; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + while (true) { + int opt = menu(scanner); + switch (opt) { + case 1: + input(scanner); + break; + case 2: + if (score == null) { + System.out.println("请先输入分数,然后执行该功能"); + break; + } + System.out.println("班级平均成绩是:" + avgScore()); + break; + case 3: + if (score == null) { + System.out.println("请先输入分数,然后执行该功能"); + break; + } + System.out.println("班级最高成绩是:" + highestScore()); + break; + case 4: + if (score == null) { + System.out.println("请先输入分数,然后执行该功能"); + break; + } + System.out.println("班级最低成绩是:" + lowestScore()); + break; + case 5: + if (score == null) { + System.out.println("请先输入分数,然后执行该功能"); + break; + } + sortScores(); + break; + default: + return; + } + } + } + + public static int menu(Scanner scanner){ + System.out.println("1.输入学生成绩"); + System.out.println("2.获取班级平均成绩"); + System.out.println("3.获取班级最高分"); + System.out.println("4.获取班级最低分"); + System.out.println("5.排序"); + System.out.println("6.退出"); + int opt = -1; + while (opt == -1) { + System.out.print(">>>请输入选项:"); + opt = checkInputNumber(scanner, 1, 6); + } + return opt; + } + + public static void input(Scanner scanner){ + score = new int[5]; + scanner = new Scanner(System.in); + for (int i = 0; i < score.length; i++) { + int tmp = -1; + while (tmp == -1) { + System.out.println("请输入第" + (i+1) + "个成绩:"); + tmp = checkInputNumber(scanner,0,100); + } + score[i] = tmp; + } + System.out.println("输入完毕!"); + } + + public static double avgScore(){ + double sum = 0; + for (int i = 0; i < score.length; i++) { + sum += score[i]; + } + return sum / score.length; + } + + public static int highestScore(){ + int max = score[0]; + for (int i = 0; i < score.length; i++) { + if(score[i] > max) + max = score[i]; + } + return max; + } + + public static int lowestScore(){ + int min = score[0]; + for (int i = 0; i < score.length; i++) { + if(score[i] < min) + min = score[i]; + } + return min; + } + + public static void sortScores(){ + Arrays.sort(score); + System.out.println("学生成绩从高到低依次为:"); + for (int i = score.length-1; i >= 0 ; i--) { + System.out.print(score[i] + " "); + } + System.out.println(); + } + + 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; + } +}