2022/09/15 第三周
This commit is contained in:
parent
81a1888519
commit
a74680ee76
1
.idea/.name
generated
1
.idea/.name
generated
@ -1 +0,0 @@
|
||||
Demo1.java
|
1
.idea/modules.xml
generated
1
.idea/modules.xml
generated
@ -3,6 +3,7 @@
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Week2/Week2.iml" filepath="$PROJECT_DIR$/Week2/Week2.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week3/Week3.iml" filepath="$PROJECT_DIR$/Week3/Week3.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/java_class.iml" filepath="$PROJECT_DIR$/java_class.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/week1/week1.iml" filepath="$PROJECT_DIR$/week1/week1.iml" />
|
||||
</modules>
|
||||
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
11
Week3/Week3.iml
Normal file
11
Week3/Week3.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
25
Week3/src/pta/Main.java
Normal file
25
Week3/src/pta/Main.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
44
Week3/src/work/Exercise1.java
Normal file
44
Week3/src/work/Exercise1.java
Normal file
@ -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!");
|
||||
}
|
||||
}
|
46
Week3/src/work/Exercise2.java
Normal file
46
Week3/src/work/Exercise2.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
129
Week3/src/work/Exercise3.java
Normal file
129
Week3/src/work/Exercise3.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user