修改项目目录
This commit is contained in:
32
src/chapter2/ClassScoreAnalyzer.java
Normal file
32
src/chapter2/ClassScoreAnalyzer.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package chapter2;
|
||||
|
||||
public class ClassScoreAnalyzer {
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建并初始化数组
|
||||
int[] testScores = {85, 92, 78, 95, 88};
|
||||
|
||||
// 2. 使用循环打印所有学生的成绩
|
||||
for (int i = 0; i < testScores.length; i++) {
|
||||
System.out.println("学生" + (i + 1) + "的成绩:" + testScores[i]);
|
||||
}
|
||||
|
||||
// 4. 调用 findHighestScore 方法并打印结果
|
||||
int highestScore = findHighestScore(testScores);
|
||||
System.out.println("班级最高分是:" + highestScore);
|
||||
}
|
||||
|
||||
// 3. 编写 findHighestScore 方法
|
||||
public static int findHighestScore(int[] scores) {
|
||||
if (scores == null || scores.length == 0) {
|
||||
return -1; // 返回-1表示数组为空或null
|
||||
}
|
||||
|
||||
int max = scores[0]; // 假设第一个元素是最大的
|
||||
for (int i = 1; i < scores.length; i++) {
|
||||
if (scores[i] > max) {
|
||||
max = scores[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
40
src/chapter2/Department.java
Normal file
40
src/chapter2/Department.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package chapter2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Department {
|
||||
/*
|
||||
* 根据用户输入的信息确定员工应分配到那个部门。
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// 声明一个 Scanner 对象,用来读取用户的输入
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
System.out.println("请输入员工的姓名");
|
||||
String name = input.nextLine();
|
||||
|
||||
System.out.println("请输入新员工应聘的语言:");
|
||||
String language = input.nextLine();
|
||||
|
||||
switch (language) {
|
||||
case "java":
|
||||
case "Java":
|
||||
System.out.println(name + " 被分配到Java程序开发部门");
|
||||
break;
|
||||
case "C#":
|
||||
case "c#":
|
||||
System.out.println(name + " 被分配到C#程序开发部门");
|
||||
break;
|
||||
case "asp.net":
|
||||
System.out.println(name + "被分配到asp.net程序测试");
|
||||
break;
|
||||
case "html":
|
||||
case "HTML":
|
||||
System.out.println(name + "被分配到前端程序开发部门");
|
||||
break;
|
||||
default:
|
||||
System.out.println("抱歉,本公司不需要" + language + "语言的人");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/chapter2/Example07.java
Normal file
12
src/chapter2/Example07.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example07 {
|
||||
public static void main(String[] args) {
|
||||
int x = 5;
|
||||
if (x < 10) {
|
||||
x ++; // x = x + 1;
|
||||
}
|
||||
|
||||
System.out.println("x=" + x);
|
||||
}
|
||||
}
|
||||
13
src/chapter2/Example08.java
Normal file
13
src/chapter2/Example08.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example08 {
|
||||
public static void main(String[] args) {
|
||||
int num = 20;
|
||||
|
||||
if (num % 2 == 0) {
|
||||
System.out.println("num是一个偶数");
|
||||
} else {
|
||||
System.out.println("num是一个奇数");
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/chapter2/Example09.java
Normal file
16
src/chapter2/Example09.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example09 {
|
||||
public static void main(String[] args) {
|
||||
int grade = 75;
|
||||
if (grade > 80) {
|
||||
System.out.println("该成绩的等级为优");
|
||||
} else if (grade > 70) {
|
||||
System.out.println("该成绩的等级是良");
|
||||
} else if (grade > 60) {
|
||||
System.out.println("该成绩的等级是中");
|
||||
} else {
|
||||
System.out.println("该成绩的等级是差");
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/chapter2/Example10.java
Normal file
13
src/chapter2/Example10.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example10 {
|
||||
public static void main(String[] args) {
|
||||
int x = 0;
|
||||
int y = 10;
|
||||
int max;
|
||||
|
||||
max = x > y ? 100 : 1000;
|
||||
|
||||
System.out.println("max=" + max);
|
||||
}
|
||||
}
|
||||
33
src/chapter2/Example11.java
Normal file
33
src/chapter2/Example11.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example11 {
|
||||
public static void main(String[] args) {
|
||||
int week = 1;
|
||||
|
||||
switch (week) {
|
||||
case 1:
|
||||
System.out.println("星期一");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("星期二");
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("星期三");
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("星期四");
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("星期五");
|
||||
break;
|
||||
case 6:
|
||||
System.out.println("星期六");
|
||||
break;
|
||||
case 7:
|
||||
System.out.println("星期日");
|
||||
break;
|
||||
default:
|
||||
System.out.println("数值错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/chapter2/Example12.java
Normal file
11
src/chapter2/Example12.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example12 {
|
||||
public static void main(String[] args) {
|
||||
int x = 5; // 定义变量x,初始值为1
|
||||
while (x <= 4) { // // 循环条件
|
||||
System.out.println("x = " + x); // 条件成立,打印x的值
|
||||
x ++; // x进行自增
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/chapter2/Example13.java
Normal file
11
src/chapter2/Example13.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example13 {
|
||||
public static void main(String[] args) {
|
||||
int x = 5;
|
||||
do {
|
||||
System.out.println("x = " + x);
|
||||
x ++;
|
||||
} while (x <= 4);
|
||||
}
|
||||
}
|
||||
11
src/chapter2/Example14.java
Normal file
11
src/chapter2/Example14.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example14 {
|
||||
public static void main(String[] args) {
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
sum += i;
|
||||
}
|
||||
System.out.println("sum = " + sum);
|
||||
}
|
||||
}
|
||||
14
src/chapter2/Example16.java
Normal file
14
src/chapter2/Example16.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example16 {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
||||
while (x <= 4) {
|
||||
System.out.println("x = " + x);
|
||||
if (x == 3) {
|
||||
continue;
|
||||
}
|
||||
x ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/chapter2/Example17.java
Normal file
14
src/chapter2/Example17.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example17 {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
||||
while (x <= 4) {
|
||||
System.out.println("x = " + x);
|
||||
if (x == 3) {
|
||||
continue;
|
||||
}
|
||||
x ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/chapter2/Example19.java
Normal file
18
src/chapter2/Example19.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example19 {
|
||||
public static void main(String[] args) {
|
||||
printRectangle(3, 5);
|
||||
}
|
||||
|
||||
// 下面定义了一个打印矩形的方法,接收两个参数,其中height为高,width为宽
|
||||
public static void printRectangle(int height, int width) {
|
||||
// 下面是使用嵌套for循环实现*打印矩形
|
||||
for (int i = 0; i < height; i++) {
|
||||
for (int j = 0; j < width; j++) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
13
src/chapter2/Example20.java
Normal file
13
src/chapter2/Example20.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example20 {
|
||||
public static void main(String[] args) {
|
||||
int area = getArea(3, 5);
|
||||
System.out.println("The area is " + area);
|
||||
}
|
||||
|
||||
public static int getArea(int x, int y) {
|
||||
int temp = x * y;
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
29
src/chapter2/Example21.java
Normal file
29
src/chapter2/Example21.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example21 {
|
||||
public static void main(String[] args) {
|
||||
// 下面针对求和方法的调用
|
||||
int sum1 = add(1, 2);
|
||||
int sum2 = add(2, 2, 3);
|
||||
double sum3 = add(1.2, 2.3);
|
||||
// 下面的代码是打印求和的结果
|
||||
System.out.println("sum1 = " + sum1);
|
||||
System.out.println("sum2 = " + sum2);
|
||||
System.out.println("sum3 = " + sum3);
|
||||
}
|
||||
|
||||
// 下面的方法实现了两个整数相加
|
||||
public static int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// 下面的方法实现了三个整数相加
|
||||
public static int add(int a, int b, int c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
// 下面的方法实现了两个小数相加
|
||||
public static double add(double a, double b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
15
src/chapter2/Example22.java
Normal file
15
src/chapter2/Example22.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example22 {
|
||||
public static void main(String[] args) {
|
||||
int[] arr; // 声明变量
|
||||
arr = new int[3]; // 创建数据对象
|
||||
|
||||
System.out.println("arr[0] = " + arr[0]); // 访问数组中的第一个元素
|
||||
System.out.println("arr[1] = " + arr[1]); // 访问数组中的第二个元素
|
||||
System.out.println("arr[2] = " + arr[2]); // 访问数组中的第三个元素
|
||||
|
||||
System.out.println("数组的长度是:" + arr.length); // 打印数组的长度
|
||||
|
||||
}
|
||||
}
|
||||
14
src/chapter2/Example23.java
Normal file
14
src/chapter2/Example23.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example23 {
|
||||
public static void main(String[] args) {
|
||||
int[] arr = new int[4]; // 定义可以存储4个元素的整数类型数组
|
||||
arr[0] = 1; // 为第1个元素赋值1
|
||||
arr[1] = 1; // 为第2个元素赋值2
|
||||
//依次打印数组中每个元素的值
|
||||
System.out.println("arr[0]: " + arr[0]);
|
||||
System.out.println("arr[1]: " + arr[1]);
|
||||
System.out.println("arr[2]: " + arr[2]);
|
||||
System.out.println("arr[3]: " + arr[3]);
|
||||
}
|
||||
}
|
||||
7
src/chapter2/GetMax.java
Normal file
7
src/chapter2/GetMax.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package chapter2;
|
||||
|
||||
public class GetMax {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
55
src/chapter2/SuperMarket.java
Normal file
55
src/chapter2/SuperMarket.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package chapter2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SuperMarket {
|
||||
/*
|
||||
* 模拟商城购物小系统:
|
||||
* 1.用户选择商品后,后台计算商品价格;
|
||||
* 2.使用while循环实现用户多次购买商品。
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
double cup = 18.8; //水杯价格
|
||||
double apple = 12.5; //苹果价格
|
||||
double banana = 15.5; //香蕉价格
|
||||
int i = 0; // 用户购买的商品序列
|
||||
|
||||
String continueShopping = "Y"; // 是否继续购物
|
||||
System.out.println("==============天津现代小卖部================");
|
||||
System.out.println("1.水杯的价格为:" + cup + "元");
|
||||
System.out.println("2.苹果的价格为:" + apple + "元");
|
||||
System.out.println("3.香蕉的价格为:" + banana + "元");
|
||||
|
||||
while (continueShopping.equals("Y")) {
|
||||
Scanner sc1 = new Scanner(System.in);
|
||||
System.out.println("请输入你需要购买商品的序列号:");
|
||||
i = sc1.nextInt();
|
||||
|
||||
switch (i) {
|
||||
case 1:
|
||||
System.out.println("请输入你需要购买水杯的数量:");
|
||||
int cupnumber = sc1.nextInt();
|
||||
double cupnum = cupnumber * cup;
|
||||
System.out.println("你购买了水杯" + cupnumber + "个,需要花费" + cupnum + "元");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("请输入你需要购买苹果的数量:");
|
||||
int applenumber = sc1.nextInt();
|
||||
double applenum = applenumber * apple;
|
||||
System.out.println("你购买了苹果" + applenumber + "斤,需要花费" + applenum + "元");
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("请输入你需要购买香蕉的数量:");
|
||||
int banananumber = sc1.nextInt();
|
||||
double banananum = banananumber * banana;
|
||||
System.out.println("你购买了香蕉" + banananumber + "斤,需要花费" + banananum + "元");
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.println("需要继续购买请输入Y,否则输入N");
|
||||
continueShopping = sc1.next();
|
||||
}
|
||||
|
||||
System.out.println("期待您的下次光临!");
|
||||
}
|
||||
}
|
||||
122
src/chapter2/User.java
Normal file
122
src/chapter2/User.java
Normal file
@@ -0,0 +1,122 @@
|
||||
package chapter2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class User {
|
||||
// 存储用户名和密码
|
||||
// 两个数组前加上static属性,这样做是为了让本类都可以使用到这两个数组
|
||||
static String[] usernames = new String[10];
|
||||
static String[] passwords = new String[10];
|
||||
|
||||
static int count = 1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 假设系统中已经有一个用户
|
||||
usernames[0] = "java";
|
||||
passwords[0] = "123456";
|
||||
|
||||
while (true) {
|
||||
print();
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("请选择功能:");
|
||||
int option = input.nextInt();
|
||||
switch (option) {
|
||||
case 0:
|
||||
System.exit(0);
|
||||
case 1:
|
||||
login();
|
||||
break;
|
||||
case 2:
|
||||
register();
|
||||
break;
|
||||
case 3:
|
||||
show();
|
||||
break;
|
||||
default:
|
||||
System.out.println("输入错误!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 登录功能,键盘输入用户名与密码
|
||||
* 使用for循环加if判断验证输入的用户名与密码是否正确
|
||||
*/
|
||||
public static void login() {
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
System.out.println("请输入用户名:");
|
||||
String username = input.next();
|
||||
|
||||
System.out.println("请输入密码:");
|
||||
String password = input.next();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (username.equals(usernames[i]) && password.equals(passwords[i])) {
|
||||
System.out.println("登录成功!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("登录失败,请重新输入");
|
||||
}
|
||||
|
||||
/*
|
||||
* 注册功能
|
||||
* 定义一个boolean变量,使用for循序判断用户是否存在
|
||||
* 如果用户不存在,在数组中插入注册的账号密码时
|
||||
* 可能会有数组长度不够的情况,所以需要使用到
|
||||
* 增加数组的长度,这里调用增加数组长度的方法add()。
|
||||
*/
|
||||
public static void register() {
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
System.out.println("请输入用户名:");
|
||||
String username = input.next();
|
||||
|
||||
System.out.println("请输入密码:");
|
||||
String password = input.next();
|
||||
|
||||
boolean flag = true;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (username.equals(usernames[i])) {
|
||||
System.out.println("用户名已存在");
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
if (count < usernames.length) {
|
||||
count++;
|
||||
usernames[count - 1] = username;
|
||||
passwords[count - 1] = password;
|
||||
System.out.println("注册成功!");
|
||||
} else {
|
||||
System.out.println("数组空间不足!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 查看功能
|
||||
*/
|
||||
public static void show() {
|
||||
for (int i = 0; i < count; i++) {
|
||||
System.out.println("用户名:" + usernames[i] + ",密码:" + passwords[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 打印功能菜单
|
||||
*/
|
||||
public static void print() {
|
||||
System.out.println("-----------------用户管理系统-------------------");
|
||||
System.out.println("1.登录功能");
|
||||
System.out.println("2.注册功能");
|
||||
System.out.println("3.查看");
|
||||
System.out.println("0.退出");
|
||||
System.out.println("-----------------用户管理系统-------------------");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user