2022/09/09 第一次提交

This commit is contained in:
zyx 2022-09-09 15:52:46 +08:00
commit 81a1888519
15 changed files with 518 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
Demo1.java

6
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

10
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Week2/Week2.iml" filepath="$PROJECT_DIR$/Week2/Week2.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>
</component>
</project>

11
Week2/Week2.iml Normal file
View 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>

43
Week2/src/Demo01.java Normal file
View File

@ -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!");
}
}

49
Week2/src/work/Work1.java Normal file
View File

@ -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!");
}
}

43
Week2/src/work/Work2.java Normal file
View File

@ -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;
}
}

View File

@ -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;
}
}

11
java_class.iml Normal file
View 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>

29
week1/src/Demo1.java Normal file
View File

@ -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);
}
}

20
week1/src/Demo2.java Normal file
View File

@ -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 + "个盒子");
}
}

25
week1/src/work/Work1.java Normal file
View File

@ -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);
}
}

67
week1/src/work/Work2.java Normal file
View File

@ -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;
}
}

11
week1/week1.iml Normal file
View 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>