java SE 周末总结
2016-11-20 22:14
232 查看
1. 查找算法
{2,30,3,45,65}
思路:首先需要遍历数组,在遍历过程中判断,使用boolean变量存储查找结果
2. 排序算法
冒泡排序:
思路:使用双重循环,外部循环控制比较轮数,内部循环控制比较次数。每次比较都是相邻两两比较,大的往后移动,移动数据需要中间变量。
冒泡排序每轮找出一个最大值
比较总轮数:数组长度 -1
每轮比较的次数:数组长度-轮数
public static void bubbleSort(int[] num) {
// 冒泡排序
for (int i = 0; i < num.length - 1; i++) {// 比较总轮数:数组长度 -1
for (int j = 0; j < num.length - (i + 1); j++) {//每轮比较的次数:数组长度-轮数
if (num[j] > num[j + 1]) {
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
然后是自己锻炼思维,做的一个简单的图书借阅系统
效果如下:
********图书管理系统*********
101 <红楼梦> 可借
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:1
************借书界面*************
请输入编号:104
输入编号不存在!
是否继续(y /n):y
************借书界面*************
请输入编号:102
该书已借出!
是否继续(y /n):y
************借书界面*************
请输入编号:101
操作成功!你已借书<红楼梦>。
是否继续(y /n):n
********图书管理系统*********
101 <红楼梦> 借出
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:2
************还书界面*************
请输入编号:101
101 <红楼梦> time-2016.11.16
是否还书(y/n):n
********图书管理系统*********
101 <红楼梦> 借出
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:2
************还书界面*************
请输入编号:101
101 <红楼梦> time-2016.11.16
是否还书(y/n):y
*********打印小票*********
编号:101
书名:<红楼梦>
借书时间:2016.11.16
还书时间:2016.11.18
总计金额:¥2元
输入任意键返回主界面:0
********图书管理系统*********
101 <红楼梦> 可借
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:
代码: 基本上写了注释的,应该可以看懂的
package com.mantou.test_2016_11_18;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SimpleLibraryProject {
public static Scanner input = new Scanner(System.in);
public static int[] num = new int[] { 101, 102, 103 };// 存放编号的数组
public static String[] strName = new String[] { "<红楼梦>", "<西游记>", "<水浒传>" };// 存放书名的数组
public static int[] state = new int[] { 0, 1, 0 };// 状态数组,0表示书没借出,1表示书已经借出
public static String[] times = new String[] { null, "2016-11-16", null };// 存放借出书时间的数组
public static int choose; // 全局变量,表示用户选择(eg:1.借书 2.还书 3.退出)
public static int chooseNum; // 用户选择借书输入的编号
public static char isContinue; // 用户选择是否继续操作
public static String returnTime;// 还书的时间
public static void start() throws ParseException {
BooksManagementSystemInterface();
}
public static void BooksManagementSystemInterface() throws ParseException {
// 图书管理系统的界面
System.out.println("**********图书管理系统*********");
for (int i = 0; i < num.length; i++) {
// 判断并输出 还书还是借书
String stateStr = "";
if (state[i] == 0) {
stateStr = "可借";
} else if (state[i] == 1) {
stateStr = "已借出";
}
System.out.println(num[i] + "\t" + strName[i] + " " + stateStr);
}
System.out.println("*******************************");
System.out.println("1." + "\t" + "借书");
System.out.println("2." + "\t" + "还书");
System.out.println("3." + "\t" + "退出");
System.out.print("请选择:");
choose = input.nextInt();
if (choose == 1) {
borrowInterface();
} else if (choose == 2) {
returnBook();
} else if (choose == 3) {
System.out.println("退出程序");
}
}
public static void borrowInterface() throws ParseException {
// 借书界面
int i = 0;
System.out.println("*********借书界面**********");
System.out.print("请输入编号:");
chooseNum = input.nextInt();
boolean bool = false;
for (i = 0; i < num.length; i++) {
// 查找用户输入的编号是否在已知数组当中
if (chooseNum == num[i]) {
bool = true; // 找到返回false ,并且结束循环
break;
} else {
bool = false; // 没有找到返回 true
}
}
if (bool == false) {
System.out.println("输入编号不存在!:");
} else {
if (state[i] == 1) {
System.out.println("该书已经借出!");
} else if (state[i] == 0) {
// 如果状态为0 说明书没有借出,用户操作成功
System.out.println("操作成功!你已借出" + strName[i]);
String time = getBorrowSystemTime();// 记录借书的时间
times[i] = time;// 存入定义好的时间数组
state[i] = 1;// 改变状态变量 ,说明书已经借出
}
4000
}
System.out.print("是否继续(y/n):");
isContinue = input.next().charAt(0);
if (isContinue == 'y') {
borrowInterface();
} else if (isContinue == 'n') {
BooksManagementSystemInterface();
}
}
public static void returnBook() throws ParseException {
// 还书功能
System.out.println("*********还书界面**********");
System.out.print("请输入编号:");
chooseNum = input.nextInt();
int temp = 0;// 中间变量,用来记录用户输入编号在数组当中的位置。
for (int i = 0; i < num.length; i++) {
// 查找用户输入编号,在数组当中的位置,用temp记录其下标
if (chooseNum == num[i]) {
temp = i;
break;
}
}
System.out.println(num[temp] + "\t" + strName[temp] + " " + times[temp]);
System.out.print("是否还书(y/n):");
char returnbook = input.next().charAt(0);
if (returnbook == 'y') {
printTicket(temp);
} else if (returnbook == 'n') {
BooksManagementSystemInterface();
}
}
public static void printTicket(int temp) throws ParseException {
// 打印小票
System.out.println("**********打印小票**********");
System.out.println("编号:" + num[temp]);
System.out.println("书名:" + strName[temp]);
returnTime = getBorrowSystemTime(); // 计算还书的时间
int sum = day_day(temp); // 计算借书和还书的时间差
System.out.println("借书时间:" + times[temp]);
System.out.println("还书时间:" + returnTime);
System.out.println("总计金额:" + sum);
System.out.println("输入任意键返回主界面:");
String anyone = input.next();
BooksManagementSystemInterface();
}
public static int day_day(int temp) throws ParseException {
// 计算借书与还书的时间差
String time1 = times[temp];
String time2 = returnTime;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
long num1 = format.parse(time1).getTime();// 获取指定日期距1990年1月1日的毫秒数
long num2 = format.parse(time2).getTime();
int days = (int) ((num2 - num1) / (1000 * 60 * 60 * 24));// days中存储的就是天数差
return days;
}
public static String getBorrowSystemTime() {
// 计算借书时的时间
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// "yyyy-MM-dd HH:mm:ss"表示日期时间字符串的格式为2016-11-18
// 15:19:30,如果只要年月日就使用“yyyy-MM-dd”
String time = format.format(date);// time变量中存储的就是日期时间字符串
return time;
}
public static void main(String[] args) throws ParseException {
start();
}
}
解释 ,这里有几个知识点需要说明一下:
使用以下补充代码必须先:在java文件的上边导入以下代码
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
1.获取系统时间:
Datedate=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//"yyyy-MM-ddHH:mm:ss"表示日期时间字符串的格式为2016-11-18
15:19:30,如果只要年月日就使用“yyyy-MM-dd”
String time=format.format(date);//time变量中存储的就是日期时间字符串
2.计算两个日期字符串的天数差
Stringtime1 = "2016-11-16";
Stringtime2 = "2016-11-18";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
longnum1 = format.parse(time1).getTime();//获取指定日期距1990年1月1日的毫秒数
longnum2 = format.parse(time2).getTime();
intdays = (int) ((num2 - num1) / (1000 * 60 * 60 * 24));//days中存储的就是天数差
提示:使用以上代码需要的函数后面添加代码throws ParseException,如下:
publicstatic void main(String[] args) throws ParseException {
String time1 ="2016-11-16";
String time2 ="2016-11-18";
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd");
long num1 = format.parse(time1).getTime();//获取指定日期距1990年1月1日的毫秒数
long num2 =format.parse(time2).getTime();
int days = (int) ((num2 -num1) / (1000 * 60 * 60 * 24));//days中存储的就是天数差
}
{2,30,3,45,65}
思路:首先需要遍历数组,在遍历过程中判断,使用boolean变量存储查找结果
public static boolean SearchNum(int number, int[][] num) { //查找最大的数, 找到后返回 true boolean bool = false; for (int i = 0; i < num.length; i++) { for (int j = 0; j < num[i].length; j++) { if (number == num[i][j]) { bool = true; } } } return bool; }
2. 排序算法
冒泡排序:
思路:使用双重循环,外部循环控制比较轮数,内部循环控制比较次数。每次比较都是相邻两两比较,大的往后移动,移动数据需要中间变量。
冒泡排序每轮找出一个最大值
比较总轮数:数组长度 -1
每轮比较的次数:数组长度-轮数
public static void bubbleSort(int[] num) {
// 冒泡排序
for (int i = 0; i < num.length - 1; i++) {// 比较总轮数:数组长度 -1
for (int j = 0; j < num.length - (i + 1); j++) {//每轮比较的次数:数组长度-轮数
if (num[j] > num[j + 1]) {
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
然后是自己锻炼思维,做的一个简单的图书借阅系统
效果如下:
********图书管理系统*********
101 <红楼梦> 可借
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:1
************借书界面*************
请输入编号:104
输入编号不存在!
是否继续(y /n):y
************借书界面*************
请输入编号:102
该书已借出!
是否继续(y /n):y
************借书界面*************
请输入编号:101
操作成功!你已借书<红楼梦>。
是否继续(y /n):n
********图书管理系统*********
101 <红楼梦> 借出
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:2
************还书界面*************
请输入编号:101
101 <红楼梦> time-2016.11.16
是否还书(y/n):n
********图书管理系统*********
101 <红楼梦> 借出
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:2
************还书界面*************
请输入编号:101
101 <红楼梦> time-2016.11.16
是否还书(y/n):y
*********打印小票*********
编号:101
书名:<红楼梦>
借书时间:2016.11.16
还书时间:2016.11.18
总计金额:¥2元
输入任意键返回主界面:0
********图书管理系统*********
101 <红楼梦> 可借
102 <西游记> 借出
103 <水浒传> 可借
*****************************
1. 借书
2. 还书
3. 退出
请选择:
代码: 基本上写了注释的,应该可以看懂的
package com.mantou.test_2016_11_18;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SimpleLibraryProject {
public static Scanner input = new Scanner(System.in);
public static int[] num = new int[] { 101, 102, 103 };// 存放编号的数组
public static String[] strName = new String[] { "<红楼梦>", "<西游记>", "<水浒传>" };// 存放书名的数组
public static int[] state = new int[] { 0, 1, 0 };// 状态数组,0表示书没借出,1表示书已经借出
public static String[] times = new String[] { null, "2016-11-16", null };// 存放借出书时间的数组
public static int choose; // 全局变量,表示用户选择(eg:1.借书 2.还书 3.退出)
public static int chooseNum; // 用户选择借书输入的编号
public static char isContinue; // 用户选择是否继续操作
public static String returnTime;// 还书的时间
public static void start() throws ParseException {
BooksManagementSystemInterface();
}
public static void BooksManagementSystemInterface() throws ParseException {
// 图书管理系统的界面
System.out.println("**********图书管理系统*********");
for (int i = 0; i < num.length; i++) {
// 判断并输出 还书还是借书
String stateStr = "";
if (state[i] == 0) {
stateStr = "可借";
} else if (state[i] == 1) {
stateStr = "已借出";
}
System.out.println(num[i] + "\t" + strName[i] + " " + stateStr);
}
System.out.println("*******************************");
System.out.println("1." + "\t" + "借书");
System.out.println("2." + "\t" + "还书");
System.out.println("3." + "\t" + "退出");
System.out.print("请选择:");
choose = input.nextInt();
if (choose == 1) {
borrowInterface();
} else if (choose == 2) {
returnBook();
} else if (choose == 3) {
System.out.println("退出程序");
}
}
public static void borrowInterface() throws ParseException {
// 借书界面
int i = 0;
System.out.println("*********借书界面**********");
System.out.print("请输入编号:");
chooseNum = input.nextInt();
boolean bool = false;
for (i = 0; i < num.length; i++) {
// 查找用户输入的编号是否在已知数组当中
if (chooseNum == num[i]) {
bool = true; // 找到返回false ,并且结束循环
break;
} else {
bool = false; // 没有找到返回 true
}
}
if (bool == false) {
System.out.println("输入编号不存在!:");
} else {
if (state[i] == 1) {
System.out.println("该书已经借出!");
} else if (state[i] == 0) {
// 如果状态为0 说明书没有借出,用户操作成功
System.out.println("操作成功!你已借出" + strName[i]);
String time = getBorrowSystemTime();// 记录借书的时间
times[i] = time;// 存入定义好的时间数组
state[i] = 1;// 改变状态变量 ,说明书已经借出
}
4000
}
System.out.print("是否继续(y/n):");
isContinue = input.next().charAt(0);
if (isContinue == 'y') {
borrowInterface();
} else if (isContinue == 'n') {
BooksManagementSystemInterface();
}
}
public static void returnBook() throws ParseException {
// 还书功能
System.out.println("*********还书界面**********");
System.out.print("请输入编号:");
chooseNum = input.nextInt();
int temp = 0;// 中间变量,用来记录用户输入编号在数组当中的位置。
for (int i = 0; i < num.length; i++) {
// 查找用户输入编号,在数组当中的位置,用temp记录其下标
if (chooseNum == num[i]) {
temp = i;
break;
}
}
System.out.println(num[temp] + "\t" + strName[temp] + " " + times[temp]);
System.out.print("是否还书(y/n):");
char returnbook = input.next().charAt(0);
if (returnbook == 'y') {
printTicket(temp);
} else if (returnbook == 'n') {
BooksManagementSystemInterface();
}
}
public static void printTicket(int temp) throws ParseException {
// 打印小票
System.out.println("**********打印小票**********");
System.out.println("编号:" + num[temp]);
System.out.println("书名:" + strName[temp]);
returnTime = getBorrowSystemTime(); // 计算还书的时间
int sum = day_day(temp); // 计算借书和还书的时间差
System.out.println("借书时间:" + times[temp]);
System.out.println("还书时间:" + returnTime);
System.out.println("总计金额:" + sum);
System.out.println("输入任意键返回主界面:");
String anyone = input.next();
BooksManagementSystemInterface();
}
public static int day_day(int temp) throws ParseException {
// 计算借书与还书的时间差
String time1 = times[temp];
String time2 = returnTime;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
long num1 = format.parse(time1).getTime();// 获取指定日期距1990年1月1日的毫秒数
long num2 = format.parse(time2).getTime();
int days = (int) ((num2 - num1) / (1000 * 60 * 60 * 24));// days中存储的就是天数差
return days;
}
public static String getBorrowSystemTime() {
// 计算借书时的时间
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// "yyyy-MM-dd HH:mm:ss"表示日期时间字符串的格式为2016-11-18
// 15:19:30,如果只要年月日就使用“yyyy-MM-dd”
String time = format.format(date);// time变量中存储的就是日期时间字符串
return time;
}
public static void main(String[] args) throws ParseException {
start();
}
}
解释 ,这里有几个知识点需要说明一下:
使用以下补充代码必须先:在java文件的上边导入以下代码
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
1.获取系统时间:
Datedate=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//"yyyy-MM-ddHH:mm:ss"表示日期时间字符串的格式为2016-11-18
15:19:30,如果只要年月日就使用“yyyy-MM-dd”
String time=format.format(date);//time变量中存储的就是日期时间字符串
2.计算两个日期字符串的天数差
Stringtime1 = "2016-11-16";
Stringtime2 = "2016-11-18";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
longnum1 = format.parse(time1).getTime();//获取指定日期距1990年1月1日的毫秒数
longnum2 = format.parse(time2).getTime();
intdays = (int) ((num2 - num1) / (1000 * 60 * 60 * 24));//days中存储的就是天数差
提示:使用以上代码需要的函数后面添加代码throws ParseException,如下:
publicstatic void main(String[] args) throws ParseException {
String time1 ="2016-11-16";
String time2 ="2016-11-18";
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd");
long num1 = format.parse(time1).getTime();//获取指定日期距1990年1月1日的毫秒数
long num2 =format.parse(time2).getTime();
int days = (int) ((num2 -num1) / (1000 * 60 * 60 * 24));//days中存储的就是天数差
}