学习!!!2022黑马程序员Java学习路线图,好像跟菜鸟教程挺一致的:Java 教程 | 菜鸟教程。
黑马是真良心!视频中出现的资料全都分享了出来的!
领取方式:关注微信公众号:黑马程序员,回复关键词:领取资源02
在这个博客中黑马程序员Java基础视频教程-课程总结文档,我将徐磊老师写的xmind转为了图片方便观看。
面向对象的三大特征:封装、继承、多态
1.给module取名字
2.命名方法:都采用驼峰法,但是类名首字母要大写,普通变量首字母小写。
3.代码快速补充的书写技巧
arr.fori + 回车
len.fori + 回车
也可快速生成 i < l e n isout + 回车
出现 System.out.println();"Hello".sout + 回车
出现 System.out.println(“Hello”);Ctrl+Alt+T
,再选择for即可4.内存位置
new
出来的对象在堆空间Car c = new Car()
中的 c
一样,都是代表当前对象的地址ArrayList
集合存储的元素并不是对象本身,而是对象的地址。5.特殊字符,A=65,a=97,英文字母个数为26
Oracle官网
Java 18 and Java 17的下载地址(老师建议新手先下载JDK17,比较稳定)
在cmd中依次输入java
和javac
没有报错就表示安装成功。
查看版本号方法:输入java -version
和javac -version
即可。版本号:javac 17.0.3.1
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
注意:当JDK ≥ 11时,可以直接键入java HelloWorld.java
执行生成结果,它会自动生成一个临时、但我们看不见的.class
文件去执行。
(1) 配置java、javac环境变量
还是跟着老师的步骤,将JDK17自动加的环境变量给删了,然后手动又加入了JDK17下的bin路径。
(2) 配置JAVA_HOME环境变量
较新版的JDK只在Path中自动配置了java和javac,没有自动配置JAVA_HOME。
官网下载地址:点击进去下载
(1) 直接导入已有模块
直接导入module存在的问题:如果直接导入的module路径不是此项目路径,则将来如果误删了此module文件夹,再使用此工程会出错(因为找不到此module了)
(2) 新建模块,再将模块的src子文件夹内容复制新模块的src中
step1:新建模块取名为 new-app2,然后会自动生成 new-app2 文件夹,包含了 src子文件夹 和 new-app2.iml 文件
step2:将待导入的模块 app2 里面的 src 文件夹中所有内容复制到 new-app2 的 src子文件 中。
删除之前:(第一张图的查看位置在D:\Ideaworkspace\javasepro\.idea\modules.xml
)
彻底删除 app2模块 的步骤:
1.Remove Module
用于在项目工程中删除该模块,并且在\modules.xml
也删除
2.在工程文件夹中,删除app2模块文件夹
按下Shift + F6
,会出现3个选项,选择第3个,会同时更改模块名+文件夹名
Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。
java基本数据类型所占字节:(参考:java基本数据类型所占字节)
/**
一道面试题,问res的数据类型应该是什么?
这里res的数据类型只能是int,不能是byte,因为在表达式中byte是直接转换成功int参与计算的
*/
byte i = 1;
byte j = 2;
int res = i + j;
常用的是&&
、||
、!
sc.nextInet()
等待录入整数sc.next()
等待录入字符串
不支持double、float是因为java中对浮点数的值往往表示并不精确,比如有时候1.3会表示成1.29999999。
switch的穿透性使用:
public class LoopDemo {
public static void main(String[] args) {
// 求1-10之间的奇数和 1+3+5+7+9=25
int sum = 0;
for (int i = 1; i <= 10; i += 2){
sum += i;
}
System.out.println("sum = " + sum);
}
}
求水仙花数以及个数
package com.itheima.loop;
public class LoopDemo2 {
public static void main(String[] args) {
/* 需求:在控制台输出所有的“水仙花数”,水仙花数必须满足如下2个要求:
1.水仙花数是一个三位数
2.水仙花数的个位、十位、百位的数字立方和等于原数
3.附加还要知道水仙花数的个数
*/
int x, y, z, sum, cnt = 0; // 个十百,立方和
for (int i = 100; i < 1000; i++) {
x = i / 100;
y = i / 10 % 10;
z = i % 10;
sum = x * x * x + y * y * y + z * z * z;
if (sum == i) {
cnt++;
System.out.print(i + "\t");
}
}
System.out.println("\n水仙花个数为:" + cnt);
}
}
package com.itheima.loop;
public class WhileDemo {
public static void main(String[] args) {
/*
需求:
世界最高山峰是珠穆朗玛峰(8848.86米=8848860毫米),假如我有一张足够大的纸,
它的厚度是0.1毫米。请问,折叠多少次,可以折成珠穆朗玛峰的高度。
*/
int high = 8848860 * 10, i = 1, cnt = 0;
while (i < high) {
i *= 2;
cnt++;
}
System.out.println("需要折叠次数为:" + cnt);
System.out.println("折叠厚度为:" + i * 0.1);
}
}
重要内容:
package com.itheima.random;
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
int answer, user;
Random r = new Random();
Scanner sc = new Scanner(System.in);
answer = r.nextInt(100) + 1;
System.out.println(answer);
while (true) {
System.out.print("请您输入猜测的答案:");
user = sc.nextInt();
if (user > answer) {
System.out.println("猜测过大!");
} else if (user < answer) {
System.out.println("猜测过小!");
} else {
System.out.println("恭喜您猜对啦!正确答案为:" + answer);
break;
}
}
}
}
重要内容:
double[] score = new double[] {98.5, 90, 89.5};
double[] score = {98.5, 90, 89.5};
(常用)arr[index]
,即数组名称[索引]arr[index] = data
arr.length
index ~ [0, length-1]
,前提是元素个数>0数据类型[] 数组名
也可以写成数据类型 数组名[]
,即int[] arr <=> int arr[]
,但是更常用的是第一种int[] arr
主要内容:
int[] arr = new int[3]; // arr = {0, 0, 0}
主要内容:
arr.fori + enter
求和、最大值 + 猜数字:
package com.itheima.traverse;
import java.util.Random;
import java.util.Scanner;
public class ArrayTraverse {
public static void main(String[] args) {
int[] money = {16, 26, 36, 6, 100};
//求和 + 求最值
int sum = 0, max = 0;
for (int i = 0; i < money.length; i++) {
sum += money[i];
if (money[i] > max) {
max = money[i];
}
}
System.out.println("sum = " + sum);
System.out.println("max = " + max);
// 猜数字
Random r = new Random();
Scanner sc = new Scanner(System.in);
int[] nums = new int[5];
int num;
boolean flag = false;
for (int i = 0; i < 5; i++) {
nums[i] = r.nextInt(20) + 1; // [1, 20]
}
while (!flag) {
System.out.print("请输入一个数字:");
num = sc.nextInt();
for (int i = 0; i < nums.length; i++) {
if (num == nums[i]) {
System.out.println("运气不错,猜中了!");
System.out.println(num + "第一次出现的位置是:" + (i + 1));
for (int i1 = 0; i1 < nums.length; i1++) {
System.out.print(nums[i1] + "\t");
}
flag = true;
break;
}
}
if (!flag) {
System.out.println("未命中!请继续!");
}
}
}
}
哈哈哈,说不清楚我这个是种什么排序,但是结果也是正确的,确实没有进行相邻位置排序交换,感觉是每次小循环就确定了大循环下标 i 处的元素值。
package com.itheima.traverse;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {3, 6, 2, 13, 20, 7};
int temp; // 从大到小排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++){
if(arr[i] < arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
这个才是冒泡排序原始版,从i=0
开始(面试时也一般是从0开始)
//按照刚才那个动图进行对应
//冒泡排序两两比较的元素是没有被排序过的元素--->
public void bubbleSort(int[] array){
for(int i=0;i<array.length-1;i++){//控制比较轮次,一共 n-1 趟
for(int j=0;j<array.length-1-i;j++){//控制两个挨着的元素进行比较
if(array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
老师的冒泡排序,从i=1
开始(也需要注意)
public void bubbleSort(int[] array){
for(int i=1;i<=array.length-1;i++){//控制比较轮次,一共 n-1 趟
for(int j=0;j<array.length-i;j++){//控制两个挨着的元素进行比较
if(array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
这个跟PyCharm是一个厂家的吧?好i像是JetBrains?所以软件布局和一些快捷键、布局设置。
之前写过一个关于PyCharm的调试方式,跟这个是一样的。∴可供参考:PyCharm的Debug和中断方法
在这个博客中黑马程序员Java基础视频教程-课程总结文档
方法就是函数
void
;方法不需要参数,则形参列表可以不写。package com.itheima.method;
public class MethodDemo2 {
public static void main(String[] args) {
System.out.println(sum(2));
isOdd(3);
int[] arr = {5, 1, 8, 2, 9};
System.out.println(max(arr));
}
// 求1-n的总和
public static int sum(int n) {
int res = 1;
for (int i = 2; i <= n; i++) {
res += i;
}
return res;
}
// 判断该数是奇数还是偶数
public static void isOdd(int x) {
if (x % 2 == 0) {
System.out.println(x + "是偶数");
} else {
System.out.println(x + "是奇数");
}
}
// 求任意整型数组的最大值
public static int max(int[] arr) {
int maximum = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maximum) {
maximum = arr[i];
}
}
return maximum;
}
}
// 按照格式打印 -> 该数组内容为:[5, 1, 8, 2, 9]
public static void print(int[] arr) {
System.out.print("该数组内容为:[");
if (arr != null && arr.length > 0) {
for (int i = 0; i < arr.length; i++) {
System.out.print(i != arr.length - 1 ? arr[i] + ", " : arr[i]);
}
}
System.out.print("]\n");
}
// 查找num在arr中的第一个位置并返回下标
public static int findIndex(int[] arr, int num) {
if (arr != null && arr.length > 0) {
for (int i = 0; i < arr.length; i++) {
if (num == arr[i]) {
return i;
}
}
} else {
System.out.println("arr is empty.");
}
return -1;
}
// 比较两个数组内容是否一样
public static boolean isSame(int[] arr1, int[] arr2){
if(arr1 == null || arr2 == null || arr1.length != arr2.length){
System.out.println("arr1 == null || arr2 == null || arr1.length != arr2.length");
return false;
}else{
for (int i = 0; i < arr1.length; i++) {
if(arr1[i] != arr2[i]){
return false;
}
}
}
System.out.println("arr1与arr2的内容相同");
return true;
}
同一个类中,出现多个方法名称相同,但是形参列表是不同的,那么这些方法就是重载方法。
package com.itheima.practice;
import java.util.Scanner;
public class BuyPlaneTicket {
public static void main(String[] args) {
// 用户输入机票原价、月份、头等舱or经济舱
Scanner sc = new Scanner(System.in);
System.out.print("请输入机票原价:");
int money = sc.nextInt();
while (money <= 0){
System.out.print("机票原价不能为非正数,请重新输入:");
money = sc.nextInt();
}
System.out.print("请输入月份(1-12):");
int month = sc.nextInt();
while (month < 1 || month > 12) {
System.out.print("月份输入有误,请重新输入(1-12):");
month = sc.nextInt();
}
System.out.print("请输入仓位(头等舱0,经济舱1):");
int top = sc.nextInt();
while (top != 0 && top != 1){
System.out.print("选择头等舱或经济舱的输入有误,请重新输入0或1:");
top = sc.nextInt();
}
System.out.println("您最终的机票优惠价格是:" + discount(money, month, top));
}
public static double discount(int money, int month, int top) {
double lastMoney = 0;
switch (top) {
case 0: //头等舱
if (month >= 5 && month <= 10) {
lastMoney = money * 0.9;
} else {
lastMoney = money * 0.7;
}
break;
case 1: //经济舱
if (month >= 5 && month <= 10) {
lastMoney = money * 0.85;
} else {
lastMoney = money * 0.65;
}
break;
}
return lastMoney;
}
}
素数:曾称质数。一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫素数。
来自于一道编程题:
判断 101~200 之间有多少个素数,并输出所有素数
package com.itheima.practice;
public class FindPrimeNum {
public static void main(String[] args) {
int a = 101, b = 200;
System.out.println("\n" + a + "~" + b + "之间的素数个数为:" + findPrime(a, b));
}
public static int findPrime(int a, int b){
int cnt = 0;
if (a <= b){
boolean flag;
for (int i = a; i <= b; i++) {
flag = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0){ // 能除尽,则表示不是素数
flag = false;
break;
}
}
if(flag){
cnt++;
System.out.print(i + "\t");
}
}
}
return cnt;
}
}
package com.itheima.practice;
import java.util.Random;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
System.out.print("请输入验证码长度:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("\n生成的验证码为:" + createCode(n));
}
public static String createCode(int n){
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type){
case 0: //大写字符
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1: //小写字符
ch = (char) (r.nextInt(26) + 97);
code += ch;
break;
case 2: //数字
code += r.nextInt(10);
}
}
return code;
}
}
package com.itheima.practice;
public class CopyArray {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = new int[arr1.length];
copyArray(arr1, arr2);
System.out.println("-----------遍历输出arr1数组元素------------");
print(arr1);
System.out.println("-----------遍历输出arr2数组元素------------");
print(arr2);
}
public static void print(int[] arr) {
System.out.print("arr = [");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
}
System.out.println("]");
}
public static void copyArray(int[] arr1, int[] arr2) {
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
System.out.println("arr1 == null || arr2 == null || arr1.length != arr2.length");
} else {
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
}
}
package com.itheima.practice;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
System.out.println("请6位评委依次打分:");
Scanner sc = new Scanner(System.in);
int[] scores = new int[6];
for (int i = 0; i < 6; i++) {
scores[i] = sc.nextInt();
}
// 求出最后的除去最高最低分的平均得分
System.out.println("最终得分为:" + meanScore(scores));
}
public static double meanScore(int[] arr) {
double sum = arr[0];
int min = arr[0], max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
sum += arr[i];
}
System.out.println("最高分是:" + max);
System.out.println("最低分是:" + min);
return (sum - max - min) / (arr.length - 2);
}
}
同样操作,对 code -> encode 再 encode 就可恢复原状code(解密)
package com.itheima.practice;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入待加密的数字长度:");
int n = sc.nextInt();
int[] code = new int[n];
System.out.println("\n请输入待加密数字:");
for (int i = 0; i < n; i++) {
code[i] = sc.nextInt();
}
System.out.println("加密前:");
print(code);
// 开始加密并打印加密结果
System.out.println("加密后:");
encode(code);
print(code);
}
public static void print(int[] arr){
System.out.print("arr = [");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
}
System.out.println("]");
}
public static void encode(int[] arr){
int temp, len = arr.length;
// for(int i = 0, j = len - 1; i < j; i++, j--) 也可以写成这样
for (int i = 0; i < len / 2; i++) {
temp = arr[i];
arr[i] = (arr[len - 1 - i] + 5) % 10;
arr[len - 1 - i] = (temp + 5) % 10;
}
}
}
package com.itheima.practice;
import java.util.Random;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
int[] luckyNumbers = createLuckyNumbers();
int[] userNumbers = userInputNumbers();
System.out.println("中奖号码是:");
print(luckyNumbers);
System.out.println("您的投注号码是:");
print(userNumbers);
judge(luckyNumbers, userNumbers);
}
public static void print(int[] arr) {
System.out.print("arr = [");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
}
System.out.println("]");
}
public static int[] createLuckyNumbers() {
// 共7个数字,红色6个1~33,蓝色1个1~16
int[] arr = new int[7];
Random r = new Random();
int data;
boolean flag;
// 生成6个不重复的随机红色球数字
for (int i = 0; i < arr.length - 1; i++) {
flag = true;
while (true) {
data = r.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (data == arr[j]){
flag = false;
break;
}
}
if (flag) {
arr[i] = data;
break;
}
}
}
// 随机生成一个蓝色球数字
arr[arr.length - 1] = r.nextInt(16) + 1;
return arr;
}
public static int[] userInputNumbers(){
int[] arr = new int[7];
Scanner sc = new Scanner(System.in);
int data;
boolean flag;
for (int i = 0; i < arr.length - 1; i++) {
System.out.print("请输入第" + (i + 1) + "个红球数字(1-33,不能重复):");
while (true){
flag = true;
data = sc.nextInt();
for (int j = 0; j < i; j++) {
if(arr[j] == data){
flag = false;
break;
}
}
if(flag && data >= 1 && data <= 33){
arr[i] = data;
break;
}
System.out.print("请重新输入第" + (i + 1) + "个红球数字:");
}
}
System.out.print("请输入蓝球数字(1-16):");
arr[arr.length - 1] = sc.nextInt();
return arr;
}
public static void judge(int[] arr1, int[] arr2){
int red = 0, blue;
for (int i = 0; i < arr1.length - 1; i++) {
for (int j = 0; j < arr2.length - 1; j++) {
if(arr1[i] == arr2[j]){
red++;
break;
}
}
}
blue = arr1[arr1.length - 1] == arr2[arr2.length - 1] ? 1 : 0;
System.out.println("您命中的红球个数:" + red);
System.out.println("您命中的蓝球个数:" + blue);
if(blue == 1 && red < 3){
System.out.println("恭喜中了5元!");
}else if(blue == 1 && red == 3 || blue == 0 && red == 4){
System.out.println("恭喜中了10元!");
}else if(blue == 1 && red == 4 || blue == 0 && red == 5){
System.out.println("恭喜中了200元!");
}else if(blue == 1 && red == 5){
System.out.println("恭喜中了3000元!");
}else if(blue == 0 && red == 6){
System.out.println("恭喜中了500万元!");
}else if(blue == 1 && red == 6){
System.out.println("恭喜中了1000万元!");
}else{
System.out.println("谢谢您为福利彩票事业做出的贡献!");
}
}
}
Goods.java 文件内容:
package com.lwd;
public class Goods {
int id;
String name;
double price;
int buyNum;
public void show(){
System.out.println("编号\t\t名称\t\t价格\t\t数量");
System.out.println(id + "\t\t" + name + "\t\t" + price + "\t\t" + buyNum);
}
}
GoodsMain.java 文件内容:
package com.lwd;
import java.util.Scanner;
public class GoodsMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int maxLen = 20, len = 0; // 记录数组有效值长度,同时也是可插入下标
Goods[] goods = new Goods[maxLen];
while (true) {
System.out.println("-------------菜单------------");
System.out.println("添加商品:add");
System.out.println("查看商品:query");
System.out.println("修改数量:update");
System.out.println("结算价格:pay");
System.out.println("退出:exit");
System.out.println("-----------------------------");
System.out.print("请您选择要操作的功能:");
String op = sc.next();
switch (op) {
case "add":
if (len < maxLen) {
addGoods(goods, len, sc);
len++;
} else {
System.out.println("购物车已满!不能再添加!");
}
break;
case "query":
if (len <= 0) {
System.out.println("您的购物车为空,请先添加商品!");
} else {
queryGoods(goods, len);
}
break;
case "update":
if (len <= 0) {
System.out.println("您的购物车为空,请先添加商品!");
} else {
System.out.print("请输入您待修改数量的编号:");
int id = sc.nextInt();
updateGoods(goods, len, id, sc);
}
break;
case "pay":
if (len <= 0) {
System.out.println("您的购物车为空,请先添加商品!");
} else {
payGoods(goods, len);
}
break;
case "exit":
System.out.println("您已退出!");
return;
default:
System.out.println("您的选择有误,请重新选择操作!");
}
}
}
public static void payGoods(Goods[] goods, int len) {
System.out.println("-------------结算pay------------");
System.out.println("编号\t\t名称\t\t价格\t\t数量");
double sum = 0;
for (int i = 0; i < len; i++) {
System.out.println(goods[i].id + "\t\t" + goods[i].name + "\t\t" + goods[i].price + "\t\t" + goods[i].buyNum);
sum += (goods[i].price * goods[i].buyNum);
}
System.out.println("总金额是:" + sum);
System.out.println("--------------------------------");
}
public static void updateGoods(Goods[] goods, int len, int id, Scanner sc) {
System.out.println("-------------修改商品update------------");
int i;
for (i = 0; i < len; i++) {
if (goods[i].id == id) {
break;
}
}
if (i == len) {
System.out.println("购物车中没有该编号的商品!");
} else {
goods[i].show();
System.out.print("请输入新的商品数量:");
goods[i].buyNum = sc.nextInt();
System.out.println("修改成功!");
goods[i].show();
}
System.out.println("----------------------------------");
}
public static void queryGoods(Goods[] goods, int len) {
System.out.println("------------查询商品query-----------");
System.out.println("编号\t\t名称\t\t价格\t\t数量");
for (int i = 0; i < len; i++) {
System.out.println(goods[i].id + "\t\t" + goods[i].name + "\t\t" + goods[i].price + "\t\t" + goods[i].buyNum);
}
System.out.println("----------------------------------");
}
public static void addGoods(Goods[] goods, int len, Scanner sc) {
System.out.println("-------------添加商品add------------");
goods[len] = new Goods();
System.out.print("请您输入商品编号(不重复):");
goods[len].id = sc.nextInt();
System.out.print("请您输入商品名称:");
goods[len].name = sc.next();
System.out.print("请您输入商品价格:");
goods[len].price = sc.nextDouble();
System.out.print("请您输入购买商品的数量:");
goods[len].buyNum = sc.nextInt();
System.out.println("添加成功!");
goods[len].show();
System.out.println("----------------------------------");
}
}
实现效果:
构造器:
this关键字:
Car c = new Car()
中的 c
一样,都是代表当前对象的地址封装的概念:
如何更好地封装:(学会使用 private
)
学会巧用 Alt+Insert(或者右键点击 Generate)
,直接 generate 已有的私有变量的get、set函数、有参、无参构造器等。
综合案例:
字符串对象的特点(面试常问):
""
直接给出来的,就一定是放在堆内存的;反之如果是用""
括起来的就是放在字符串常量池的。package com.lwd.prctice;
import java.util.Random;
import java.util.Scanner;
public class CodeTest {
public static void main(String[] args) {
// 1.构造 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
String code = "";
for (int i = 0; i < 26; i++) {
code += (char) ('a' + i);
}
for (int i = 0; i < 26; i++) {
code += (char) ('A' + i);
}
code += "0123456789";
System.out.println("code = \"" + code + "\"");
// 2.构造指定长度的验证码
System.out.print("请输入验证码位数:");
Scanner sc = new Scanner(System.in);
int len = sc.nextInt(), index;
Random r = new Random();
String code2 = "";
for (int i = 0; i < len; i++) {
index = r.nextInt(code.length());
code2 += code.charAt(index);
}
System.out.println("验证码为:" + code2);
}
}
package com.lwd.prctice;
import java.util.Scanner;
public class LoginTest {
public static void main(String[] args) {
// 1.自定义正确的用户名和密码
String okLoginName = "admin";
String okPassWord = "lwd";
// 2.循环控制3次判断登陆是否成功
Scanner sc = new Scanner(System.in);
String name = "", pwd = "";
for (int i = 1; i <= 3; i++) {
System.out.print("请输入用户名:");
name = sc.next();
System.out.print("请输入密码:");
pwd = sc.next();
if (name.equals(okLoginName)) {
if (pwd.equals(okPassWord)) {
System.out.println("登陆成功!");
break;
} else {
System.out.println("您的密码输入有误!您还有" + (3 - i) + "次机会!");
}
} else {
if (pwd.equals(okPassWord)) {
System.out.println("您的用户名输入有误!您还有" + (3 - i) + "次机会!");
} else {
System.out.println("您的用户名、密码都输入有误!您还有" + (3 - i) + "次机会!");
}
}
}
}
}
package com.lwd.prctice;
import java.util.Scanner;
public class SplitTest {
public static void main(String[] args) {
System.out.print("请您输入电话号码(11位):");
Scanner sc = new Scanner(System.in);
String phone = sc.next();
String new_phone = phone.substring(0, 4) + "****" + phone.substring(7);
System.out.println(new_phone);
}
}
特点:
String
、Integer
ArrayList list = new ArrayList<>();
i--
,使得能下标回退不会跳过此处数据(而且list.size()
一直在变,i的取值范围因此在一直缩小)package com.lwd.ArrayList;
import java.util.ArrayList;
public class RemoveTest {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(98);
list.add(77);
list.add(66);
list.add(89);
list.add(79);
list.add(50);
list.add(100);
// 1.方法1:正向从左到右删除,但是循环体内下标i--,使得能下标回退不会跳过此处数据
for (int i = 0; i < list.size(); i++) {
if(list.get(i) < 80){
list.remove(i);
i--;
}
}
System.out.println("list = " + list);
// 2.方法2:逆行从右到左删除,循环体内不做其余操作
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) < 80){
list.remove(i);
}
}
System.out.println("list = " + list);
}
}
package com.lwd.prctice;
import java.util.ArrayList;
public class ObjectTest {
public static void main(String[] args) {
ArrayList<Movie> movies= new ArrayList<>();
Movie m1 = new Movie("《肖申克的救赎》", 9.7, "罗宾斯");
Movie m2 = new Movie("《霸王别姬》", 9.6, "张国荣、张丰毅");
Movie m3 = new Movie("《阿甘正传》", 9.5, "汤姆.汉克斯");
movies.add(m1);
movies.add(m2);
movies.add(m3);
for (int i = 0; i < movies.size(); i++) {
System.out.println("--------------------");
Movie movie = movies.get(i);
System.out.println("片名:" + movie.getName());
System.out.println("评分:" + movie.getScore());
System.out.println("主演:" + movie.getActor());
}
}
}
package com.lwd.prctice;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManageTest {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("20180302", "叶孤城", 23, "护理一班"));
students.add(new Student("20180303", "东方不败", 23, "推拿二班"));
students.add(new Student("20180304", "西门吹雪", 26, "中药学四班"));
students.add(new Student("20180305", "梅超风", 26, "神经科二班"));
// 打印学生信息
printStudents(students);
// 根据学号查找学生
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("请输入待查找的学生学号:");
String id = sc.next();
findStuById(students, id);
}
}
public static Student findStuById(ArrayList<Student> students, String id) {
Student s;
for (int i = 0; i < students.size(); i++) {
s = students.get(i);
if(s.getStuId().equals(id)){
System.out.println("学号\t\t姓名\t\t年龄\t\t班级");
System.out.println(s.getStuId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getClassNum());
return s;
}
}
System.out.println("查无此人!");
return null;
}
public static void printStudents(ArrayList<Student> students) {
System.out.println("学号\t\t姓名\t\t年龄\t\t班级");
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.getStuId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getClassNum());
}
}
}
Account.java
package com.ATM;
public class Account {
private String cardId; // 卡号
private String userName; // 用户名
private String password; // 密码
private double money; // 账户余额
private double quotaMoney; // 每次取现额度
public Account() {
}
public Account(String cardId, String userName, String password, double money, double quotaMoney) {
this.cardId = cardId;
this.userName = userName;
this.password = password;
this.money = money;
this.quotaMoney = quotaMoney;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}
ATMSystem.java
package com.ATM;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args) {
// 1.定义账户集合
ArrayList<Account> accounts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int command;
while (true) {
// 展示系统首页
System.out.println("\n==================欢迎来到ATM首页====================");
System.out.println("1、账户登录");
System.out.println("2、账户开户");
System.out.println("请您选择操作:");
command = sc.nextInt();
switch (command) {
case 1:
// 账户登录
login(accounts, sc);
break;
case 2:
// 账户开户
register(accounts, sc);
break;
default:
System.out.println("您的选择有误!");
}
}
}
// 登录
private static void login(ArrayList<Account> accounts, Scanner sc) {
if (accounts.size() == 0) {
System.out.println("当前没有帐户,请先开户!");
return;
}
System.out.println("\n=================系统登录操作================");
Account account;
// 输入卡号
while (true) {
System.out.print("请您输入卡号:");
String cardId = sc.next();
account = getAccountByCardId(cardId, accounts);
if (account == null) {
System.out.println("该登陆卡号不存在,请重新操作!");
} else {
break;
}
}
// 输入正确密码
while (true) {
System.out.print("请您输入密码:");
String password = sc.next();
if (account.getPassword().equals(password)) {
break;
} else {
System.out.println("密码错误,请重新操作!");
}
}
System.out.println(account.getUserName() + "先生/女士,您已登录成功!您的卡号为:" + account.getCardId());
// 进入到用户操作功能
showUserCommand(accounts, account, sc);
}
private static void showUserCommand(ArrayList<Account> accounts, Account account, Scanner sc) {
while (true) {
System.out.println("\n===================用户操作页==================");
System.out.println("1、查询账户");
System.out.println("2、存款");
System.out.println("3、取款");
System.out.println("4、转账");
System.out.println("5、修改密码");
System.out.println("6、退出");
System.out.println("7、注销账户");
System.out.print("请选择:");
int command = sc.nextInt();
switch (command) {
case 1: // 查询
showAccount(account);
break;
case 2: // 存款
depositMoney(account, sc);
break;
case 3: // 取款
drawMoney(account, sc);
break;
case 4: // 转账
transferMoney(accounts, account, sc);
break;
case 5: // 修改密码 + 回到首页
updatePassword(account, sc);
return;
case 6: // 退出 + 回到首页
System.out.println("退出成功!欢迎下次使用!");
return;
case 7: // 成功注销账户 + 回到首页 or 未注销 + 回到用户页
if(deleteAccount(accounts, account, sc)){
return;
}else{
break;
}
default:
System.out.println("您的输入有误!");
}
}
}
private static boolean deleteAccount(ArrayList<Account> accounts, Account account, Scanner sc) {
System.out.println("\n=================用户注销账户操作================");
System.out.println("您真的要销户吗?(y/n)");
String rs = sc.next();
switch (rs) {
case "y":
if (account.getMoney() > 0) {
System.out.println("您的余额不为0,不能销户!");
} else {
accounts.remove(account);
System.out.println("您的账户销户完成!");
}
return true;
default:
System.out.println("好的,当前账户继续保留~");
}
return false;
}
private static void updatePassword(Account account, Scanner sc) {
System.out.println("\n=================用户修改密码操作================");
while (true) {
System.out.print("请您输入原始密码:");
String password = sc.next();
if (!password.equals(account.getPassword())) {
System.out.println("你的原始密码输入有误,请重新操作!");
} else {
break;
}
}
while (true) {
System.out.print("请您输入新密码:");
String password = sc.next();
if (password.equals(account.getPassword())) {
System.out.println("新密码不能与原始密码相同!请重新操作!");
continue;
}
System.out.print("请您再次输入新密码:");
if (!password.equals(sc.next())) {
System.out.println("两次新密码输入不一致,请重新操作!");
} else {
account.setPassword(password);
System.out.println("您已成功修改登录密码为:" + password);
return;
}
}
}
private static void transferMoney(ArrayList<Account> accounts, Account account, Scanner sc) {
System.out.println("\n=================用户转账操作================");
if (accounts.size() < 2) {
System.out.println("当前系统中,不足2个账户,无法进行转账,请先去开户吧!");
return;
}
if (account.getMoney() == 0) {
System.out.println("对不起,您的余额为0,就先别转了吧~");
return;
}
while (true) {
System.out.print("请输入转账用户的卡号:");
String cardId = sc.next();
// 不能给自己转账
if (cardId.equals(account.getCardId())) {
System.out.println("对不起,您不能给自己转账!");
continue;
}
Account acc = getAccountByCardId(cardId, accounts);
if (acc == null) {
System.out.println("该卡号不存在,请重新操作!");
} else {
String userName = acc.getUserName();
String tip = "*" + userName.substring(1);
System.out.print("请您输入[" + tip + "]的姓氏:");
String preName = sc.next();
if (userName.startsWith(preName)) {
// 开始转账
System.out.print("请您输入转账金额:");
double money = sc.nextDouble();
if (money > account.getMoney()) {
System.out.println("对不起,您的余额不足,最多转账:" + account.getMoney() + "元");
} else {
account.setMoney(account.getMoney() - money);
acc.setMoney(acc.getMoney() + money);
System.out.println("您已成功给[ " + acc.getCardId() + " " + acc.getUserName() + " ]转账" + money + "元,您当前余额为:" + account.getMoney() + "元");
return;
}
} else {
System.out.println("对不起,您输入的信息有误~~");
}
}
}
}
private static void drawMoney(Account account, Scanner sc) {
System.out.println("\n=================用户取钱操作================");
// 如果当前不足100元,则无法取出
if (account.getMoney() < 100) {
System.out.println("您的余额为:" + account.getMoney() + ",不足100元无法取出!");
return;
}
while (true) {
System.out.print("请您输入取款金额:");
double money = sc.nextDouble();
if (money > account.getMoney()) {
System.out.println("您的余额为:" + account.getMoney() + "元,不足以取到" + money + "元,请调整取款金额!");
} else {
if (money > account.getQuotaMoney()) {
System.out.println("您的取款金额超出额度:" + account.getQuotaMoney() + "元,请调整取款金额!");
} else {
account.setMoney(account.getMoney() - money);
break;
}
}
}
System.out.println("您已成功取款,余额为:" + account.getMoney());
}
private static void depositMoney(Account account, Scanner sc) {
System.out.println("\n=================用户存钱操作================");
System.out.print("请您输入存款金额:");
double money = sc.nextDouble();
money += account.getMoney();
account.setMoney(money);
System.out.println("您已存款成功!");
showAccount(account);
}
private static void showAccount(Account account) {
System.out.println("\n=================当前账户信息如下================");
System.out.println("卡号:" + account.getCardId());
System.out.println("户主:" + account.getUserName());
System.out.println("余额:" + account.getMoney());
System.out.println("限额:" + account.getQuotaMoney());
}
// 开户
private static void register(ArrayList<Account> accounts, Scanner sc) {
System.out.println("\n=================系统开户操作================");
Account account = new Account();
System.out.print("请您输入姓名:");
account.setUserName(sc.next());
while (true) {
System.out.print("请您输入密码:");
String password = sc.next();
System.out.print("请您确定输入密码:");
if (password.equals(sc.next())) {
account.setPassword(password);
break;
} else {
System.out.println("两次密码输入不一致!请重新输入密码!");
}
}
System.out.print("请您输入取现额度:");
account.setQuotaMoney(sc.nextDouble());
// 生成随机一个8位卡号
String cardId = getRandomCardId(accounts);
account.setCardId(cardId);
// 添加到集合中去
accounts.add(account);
System.out.println("恭喜您," + account.getUserName() + "先生/女士,您已开户成功,您的卡号是:" + cardId + ",请您妥善保管卡号");
}
// 返回随机生成的不重复的8位卡号
private static String getRandomCardId(ArrayList<Account> accounts) {
Random r = new Random();
while (true) {
// 生成随机8位卡号
String cardId = "";
for (int i = 0; i < 8; i++) {
cardId += r.nextInt(10);
}
if (getAccountByCardId(cardId, accounts) == null) {
return cardId;
}
}
}
// 根据卡号查找账户
private static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getCardId().equals(cardId)) {
return accounts.get(i);
}
}
return null;
}
}
后续课程:
JavaSE基础加强-学习黑马程序员Java基础视频教程(P93开始)