修改项目目录
This commit is contained in:
parent
028ae17647
commit
a3e84dc516
@ -1,2 +0,0 @@
|
||||
package PACKAGE_NAME;public class Example22 {
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
package PACKAGE_NAME;public class Example23 {
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
package PACKAGE_NAME;public class GetMax {
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class ClassScoreAnalyzer {
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建并初始化数组
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Department {
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example07 {
|
||||
public static void main(String[] args) {
|
||||
int x = 5;
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example08 {
|
||||
public static void main(String[] args) {
|
||||
int num = 20;
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example09 {
|
||||
public static void main(String[] args) {
|
||||
int grade = 75;
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example10 {
|
||||
public static void main(String[] args) {
|
||||
int x = 0;
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example11 {
|
||||
public static void main(String[] args) {
|
||||
int week = 1;
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example12 {
|
||||
public static void main(String[] args) {
|
||||
int x = 5; // 定义变量x,初始值为1
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example13 {
|
||||
public static void main(String[] args) {
|
||||
int x = 5;
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example14 {
|
||||
public static void main(String[] args) {
|
||||
int sum = 0;
|
@ -1,10 +1,12 @@
|
||||
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) {
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
x ++;
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example17 {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
@ -1,6 +1,8 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example19 {
|
||||
public static void main(String[] args) {
|
||||
printRectangle(3, 40);
|
||||
printRectangle(3, 5);
|
||||
}
|
||||
|
||||
// 下面定义了一个打印矩形的方法,接收两个参数,其中height为高,width为宽
|
||||
@ -8,10 +10,8 @@ public class Example19 {
|
||||
// 下面是使用嵌套for循环实现*打印矩形
|
||||
for (int i = 0; i < height; i++) {
|
||||
for (int j = 0; j < width; j++) {
|
||||
System.out.print("*");
|
||||
|
||||
}
|
||||
// System.out.print("\n");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example20 {
|
||||
public static void main(String[] args) {
|
||||
int area = getArea(3, 5);
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
public class Example21 {
|
||||
public static void main(String[] args) {
|
||||
// 下面针对求和方法的调用
|
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) {
|
||||
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package chapter2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SuperMarket {
|
||||
@ -29,26 +31,23 @@ public class SuperMarket {
|
||||
int cupnumber = sc1.nextInt();
|
||||
double cupnum = cupnumber * cup;
|
||||
System.out.println("你购买了水杯" + cupnumber + "个,需要花费" + cupnum + "元");
|
||||
System.out.println("需要继续购买请输入Y,否则输入N");
|
||||
continueShopping = sc1.next();
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("请输入你需要购买苹果的数量:");
|
||||
int applenumber = sc1.nextInt();
|
||||
double applenum = applenumber * apple;
|
||||
System.out.println("你购买了苹果" + applenumber + "斤,需要花费" + applenum + "元");
|
||||
System.out.println("需要继续购买请输入Y,否则输入N");
|
||||
continueShopping = sc1.next();
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("请输入你需要购买香蕉的数量:");
|
||||
int banananumber = sc1.nextInt();
|
||||
double banananum = banananumber * banana;
|
||||
System.out.println("你购买了香蕉" + banananumber + "斤,需要花费" + banananum + "元");
|
||||
System.out.println("需要继续购买请输入Y,否则输入N");
|
||||
continueShopping = sc1.next();
|
||||
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("-----------------用户管理系统-------------------");
|
||||
}
|
||||
}
|
37
src/chapter3/Example01.java
Normal file
37
src/chapter3/Example01.java
Normal file
@ -0,0 +1,37 @@
|
||||
package chapter3;
|
||||
|
||||
class Student {
|
||||
String name;
|
||||
int age;
|
||||
String sex;
|
||||
|
||||
void read() {
|
||||
System.out.println("大家好,我是" + name + ",我在看书!");
|
||||
}
|
||||
|
||||
void introduce() {
|
||||
System.out.println("大家好,我叫" + name + ",我今年" + age + "岁了,我是" + sex + "孩");
|
||||
}
|
||||
}
|
||||
|
||||
public class Example01 {
|
||||
public static void main(String[] args) {
|
||||
Student s1 = new Student();
|
||||
Student s2 = new Student();
|
||||
|
||||
s1.name = "小明";
|
||||
s1.sex = "男";
|
||||
s1.age = 20;
|
||||
|
||||
|
||||
s2.name = "小红";
|
||||
s2.sex = "女";
|
||||
s2.age = 19;
|
||||
|
||||
s1.read();
|
||||
s2.read();
|
||||
|
||||
s1.introduce();
|
||||
s2.introduce();
|
||||
}
|
||||
}
|
59
src/chapter3/Example05.java
Normal file
59
src/chapter3/Example05.java
Normal file
@ -0,0 +1,59 @@
|
||||
package chapter3;
|
||||
|
||||
class Book {
|
||||
int id; // 编号
|
||||
String name; // 书名
|
||||
|
||||
// public Book() {
|
||||
// this.id = 0;
|
||||
// this.name = "未命名";
|
||||
// System.out.println("初始化:无参构造被调用!");
|
||||
// }
|
||||
|
||||
// 打印信息
|
||||
public void print() {
|
||||
System.out.println("print函数 - ID:" + this.id + ",书名:" + this.name);
|
||||
}
|
||||
|
||||
// 获取书号
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
// 设置书号
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
// 获取书名
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
// 设置书名
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Book[] books = new Book[10];
|
||||
|
||||
// 使用 setter 初始化
|
||||
books[0] = new Book();
|
||||
books[0].print();
|
||||
System.out.println("初始化:手动");
|
||||
books[0].setId(1);
|
||||
books[0].setName("Java教程");
|
||||
books[0].print();
|
||||
|
||||
System.out.println("============");
|
||||
|
||||
// 直接访问属性进行初始化(注意访问控制)
|
||||
books[1] = new Book();
|
||||
books[1].print();
|
||||
System.out.println("初始化:手动");
|
||||
books[1].id = 2;
|
||||
books[1].name = "JSP教程";
|
||||
books[1].print();
|
||||
}
|
||||
}
|
36
src/chapter3/demo31/Book.java
Normal file
36
src/chapter3/demo31/Book.java
Normal file
@ -0,0 +1,36 @@
|
||||
package chapter3.demo31;
|
||||
|
||||
public class Book {
|
||||
private final int id; // 编号
|
||||
private final String name; // 书名
|
||||
private final double price; // 价格
|
||||
private final int storage; // 库存
|
||||
|
||||
// 有参构造
|
||||
public Book(int id, String name, double price, int storage) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
// 获取书号
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
// 获取书名
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
// 获取价格
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
// 获取库存
|
||||
public int getStorage() {
|
||||
return storage;
|
||||
}
|
||||
}
|
43
src/chapter3/demo31/Order.java
Normal file
43
src/chapter3/demo31/Order.java
Normal file
@ -0,0 +1,43 @@
|
||||
package chapter3.demo31;
|
||||
|
||||
public class Order {
|
||||
private final String orderId;
|
||||
private final OrderItem[] items;
|
||||
private double total;
|
||||
|
||||
// 有参构造
|
||||
public Order(String orderId) {
|
||||
this.orderId = orderId;
|
||||
this.items = new OrderItem[3];
|
||||
}
|
||||
|
||||
// 获取订单号
|
||||
public String getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
// 获取订单列表
|
||||
public OrderItem[] getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
// 获取订单总额
|
||||
public double getTotal() {
|
||||
calTotal();
|
||||
return total;
|
||||
}
|
||||
|
||||
// 指定一个订单项
|
||||
public void setItem(OrderItem item, int i) {
|
||||
this.items[i] = item;
|
||||
}
|
||||
|
||||
// 计算订单总额
|
||||
public void calTotal() {
|
||||
double total = 0;
|
||||
for (OrderItem item : items) {
|
||||
total += item.getNum() * item.getBook().getPrice();
|
||||
}
|
||||
this.total = total;
|
||||
}
|
||||
}
|
22
src/chapter3/demo31/OrderItem.java
Normal file
22
src/chapter3/demo31/OrderItem.java
Normal file
@ -0,0 +1,22 @@
|
||||
package chapter3.demo31;
|
||||
|
||||
public class OrderItem {
|
||||
private final Book book;
|
||||
private final int num;
|
||||
|
||||
// 有参构造方法
|
||||
public OrderItem(Book book, int num) {
|
||||
this.book = book;
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
// 获取图书对象
|
||||
public Book getBook() {
|
||||
return book;
|
||||
}
|
||||
|
||||
// 获取订购图书数量
|
||||
public int getNum() {
|
||||
return num;
|
||||
}
|
||||
}
|
64
src/chapter3/demo31/PayBooks.java
Normal file
64
src/chapter3/demo31/PayBooks.java
Normal file
@ -0,0 +1,64 @@
|
||||
package chapter3.demo31;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PayBooks {
|
||||
public static void main(String[] args) {
|
||||
Book[] books = new Book[3];
|
||||
// 模拟从数据库中读取图书信息并输出
|
||||
outBooks(books);
|
||||
// 顾客购买图书
|
||||
Order order = purchase(books);
|
||||
// 输出订单信息
|
||||
outOrder(order);
|
||||
}
|
||||
|
||||
// 顾客购买图书
|
||||
public static Order purchase(Book[] books) {
|
||||
Order order = new Order("00001");
|
||||
OrderItem item = null;
|
||||
Scanner in = new Scanner(System.in);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
System.out.println("请输入图书编号选择图书:");
|
||||
int cno = in.nextInt();
|
||||
System.out.println("请输入购买图书数量:");
|
||||
int pnum = in.nextInt();
|
||||
item = new OrderItem(books[cno - 1], pnum);
|
||||
order.setItem(item, i);
|
||||
System.out.println("请继续购买图书。");
|
||||
}
|
||||
in.close();
|
||||
return order;
|
||||
}
|
||||
|
||||
// 输出订单信息
|
||||
public static void outOrder(Order order) {
|
||||
System.out.println("\n===================图书订单===================");
|
||||
System.out.println("图书订单号:" + order.getOrderId());
|
||||
System.out.println("图书名称\t购买数量\t图书单价");
|
||||
System.out.println("--------------------------------------");
|
||||
OrderItem[] items = order.getItems();
|
||||
for (OrderItem item : items) {
|
||||
System.out.println(
|
||||
item.getBook().getName() + "\t" + item.getNum() + "\t" + item.getBook().getPrice());
|
||||
}
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.println("订单总额:\t\t" + order.getTotal());
|
||||
}
|
||||
|
||||
// 模拟从数据库中读取图书信息并输出
|
||||
public static void outBooks(Book[] books) {
|
||||
books[0] = new Book(1, "Java教程", 30.6, 30);
|
||||
books[1] = new Book(2, "JSP教程", 42.1, 40);
|
||||
books[2] = new Book(3, "SSH架构", 47.3, 15);
|
||||
System.out.println("===================图书列表===================");
|
||||
System.out.println("图书编号\t图书名称\t\t图书单价\t库存数量");
|
||||
System.out.println("---------------------------------------");
|
||||
for (int i = 0; i < books.length; i++) {
|
||||
System.out.println(
|
||||
i + 1 + "\t" + books[i].getName() + "\t" + books[i].getPrice() + "\t" + books[i].getStorage());
|
||||
}
|
||||
System.out.println("---------------------------------------");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user