首页 > 代码库 > java基础的运用
java基础的运用
java基础
我们学习了java基础后要将这些知识牢记的方法只有一个就是敲代码,在这段时间学习中我们要将学过的知识融汇贯通,下面是我写的两个基础项目:
第一个是ATM的:java 代码
package projict;
import java.util.Scanner;
public class projict_ATM_ {
public static void main(String[] args) {
String[] userName = new String[10];
int[] password = new int[10];
while (true) {
System.out.println("=========欢迎使用你哥的ATM========");
System.out.println("A 登陆");
System.out.println("B 开户");
System.out.println("C 修改密码");
System.out.println("D 退出");
System.out.println("请选择菜单");
Scanner scan = new Scanner(System.in);
String operator = scan.next();
operator = operator.toUpperCase();// 大小写换
char[] chr = operator.toCharArray();// 把字符转换为char数组;
switch (chr[0]) {
case ‘A‘:
login(userName, password);
int cis = 1;
cis++;
if (cis > 3) {
System.out.println("输入次数过多账户被锁");
return;
}
break;
case ‘B‘:
oppen(userName, password);
break;
case ‘C‘:
passWord(userName, password);
break;
case ‘D‘:
System.out.println("谢谢使用!");
System.exit(-1);
default:
break;
}
}
}
// 登陆
public static void login(String[] userName, int[] password) {
System.out.println("请输入用户名:");
Scanner scan = new Scanner(System.in);
String username = scan.next();
System.out.println("请输入您的密码:");
int password2 = scan.nextInt();
int index = -1;
for (int i = 0; i < userName.length; i++) {
if (username.equals(userName[i]) && password2 == password[i]) {
System.out.println("登陆成功");
index = i;
pwdScane(userName, password);
break;
}
}
if (index == -1) {
System.out.println("请重新输入");
return;
}
}
// 开户
public static void oppen(String[] userName, int[] password) {
System.out.println("新用户名:");
Scanner scan = new Scanner(System.in);
String userName1 = scan.next();
System.out.println("新用户密码");
Scanner number = new Scanner(System.in);
int password1 = number.nextInt();
for (int i = 0; i < userName.length; i++) {
if (userName[i] == null) {
userName[i] = userName1;
password[i] = password1;
break;
}
}
System.out.println("开户成功");
}
// 修改密码
public static void passWord(String[] userName, int password[]) {
System.out.println("请输入用户名:");
Scanner scan = new Scanner(System.in);
String username = scan.next();
System.out.println("请输入您的密码:");
int password2 = scan.nextInt();
for (int i = 0; i < userName.length; i++) {
if (username.equals(userName[i]) && password2 == password[i]) {
System.out.println("请输入您的新密码");
Scanner seadm = new Scanner(System.in);
int newpass = seadm.nextInt();
password[i] = newpass;
}
System.out.println("修改成功");
break;
}
}
// 小菜单
public static void pwdScane(String[] userName, int password[]) {
int[] manny = new int[1];
while (true) {
System.out.println("====欢迎进入您的死人空间====");
System.out.println("A 查询");
System.out.println("B 存款");
System.out.println("C 取款");
System.out.println("D 转账");
System.out.println("E 返回上级菜单");
System.out.println("请选择");
Scanner scan = new Scanner(System.in);
String operator = scan.next();
operator = operator.toUpperCase();// 大小写换
char[] chr = operator.toCharArray();// 把字符转换为char数组;
switch (chr[0]) {
case ‘A‘:
query(userName, password, manny);
break;
case ‘B‘:
deposit(userName, password, manny);
break;
case ‘C‘:
withdrawals(userName, password, manny);
break;
case ‘D‘:
transfer(userName, password, manny);
break;
case ‘E‘:
return;
default:
break;
}
}
}
// 查询
public static void query(String[] userName, int password[], int[] manny) {
System.out.println("请输入您的密码");
Scanner scan = new Scanner(System.in);
int passWord = scan.nextInt();
for (int i = 0; i < password.length; i++) {
if (passWord == password[i]) {
System.out.println("您的余额为" + manny[0]);
break;
}
}
}
// 存款
public static void deposit(String[] userName, int password[], int[] manny) {
System.out.println("请输入您的密码");
Scanner scan = new Scanner(System.in);
int passWord = scan.nextInt();
for (int i = 0; i < password.length; i++) {
if (passWord == password[i]) {
System.out.println("请输入存款金额");
Scanner scane = new Scanner(System.in);
int manny1 = scane.nextInt();
manny[0] = manny1 + manny[0];
System.out.println("存入金额为" + manny1);
break;
}
System.out.println("密码错误");
break;
}
}
// 取款
public static void withdrawals(String[] userName, int password[],
int[] manny) {
System.out.println("请输入您的密码");
Scanner scan = new Scanner(System.in);
int passWord = scan.nextInt();
for (int i = 0; i < password.length; i++) {
if (passWord == password[i]) {
System.out.println("请输入您想取款金额");
System.out.println("ps:本机只有100面值请勿乱输");
Scanner scane = new Scanner(System.in);
int manny1 = scane.nextInt();
if (manny[0] > manny1) {
manny[0] = manny[0] - manny1;
System.out.println("取款成功,您的账户还有" + manny[0]);
break;
} else {
System.out.println("账户余额不足");
break;
}
}
System.out.println("密码错误");
break;
}
}
// 转账
public static void transfer(String[] userName, int password[], int[] manny) {
System.out.println("请输入您的密码");
Scanner scan = new Scanner(System.in);
int passWord = scan.nextInt();
for (int i = 0; i < password.length; i++) {
if (passWord == password[i]) {
System.out.println("请输入转账账户");
Scanner scane = new Scanner(System.in);
String username = scane.next();
System.out.println("请输入转入金额");
Scanner scana = new Scanner(System.in);
int menny = scana.nextInt();
userName[i]=username;
if (manny[0]>=menny) {
manny[0] = manny[0] - menny;
System.out.println("转账成功,剩余" + manny[0]);
break;
}
System.out.println("账户余额不足");
break;
}
}
return;
}
}
在寄出后个人觉得函数运用肯定是以后学习java必须用到的;
第二个是简易学生信息管理系统:
package work_11_;
import java.util.Scanner;
public class work_11_23_big {
public static void main(String[] ages) {
String[] user = new String[10];
int[] password = new int[10];
while (true) {
System.out.println("=======菜单======");
System.out.println("A 注册");
System.out.println("B 登陆");
System.out.println("C 退出");
System.out.println("请选择菜单");
Scanner scan = new Scanner(System.in);
String operator = scan.next();
operator = operator.toUpperCase();// 大小写换
char[] chr = operator.toCharArray();// 把字符转换为char数组;
switch (chr[0]) {
case ‘A‘:
langs(user, password);
break;
case ‘B‘:
count(user, password);
break;
case ‘C‘:
System.out.println("退出系统!");
System.exit(-1);
break;
default:
break;
}
}
}
public static void count(String[] user, int[] password) {
System.out.println("请输入用户名:");
Scanner pup = new Scanner(System.in);
String pwdname = pup.next();
System.out.println("请输入密码:");
int pap = pup.nextInt();
int index = -1;
for (int i = 0; i < user.length; i++) {
if (pwdname.equals(user[i]) && pap == password[i]) {
System.out.println("登陆成功");
pepac(user, password);
index = i;
break;
}
}
if (index == -1) {
System.out.println("登录失败");
return;
}
}
public static void langs(String[] user, int[] password) {
System.out.println("请输入用户名");
Scanner scan = new Scanner(System.in);
String userName = scan.nextLine();
System.out.println("请输入密码");
int pwd = scan.nextInt();
for (int i = 0; i < user.length; i++) {
if (user[i] == null) {
user[i] = userName;
password[i] = pwd;
break;
}
}
}
private static void pepac(String[] user, int[] pwdname) {
String[] studentName = new String[2];
int[] score = new int[2];
boolean flag = false;
while (true) {
System.out.println("=========小菜单=========");
System.out.println("A 新增学生成绩");
System.out.println("B 修改学生成绩");
System.out.println("C 删除学生成绩");
System.out.println("D 查询学生成绩");
System.out.println("E 返回上级菜单");
System.out.println("请选择菜单:");
Scanner input = new Scanner(System.in);
String operator = input.nextLine();
operator = operator.toUpperCase();
char op = operator.toCharArray()[0];
switch (op) {
case ‘A‘:
addInfo(studentName, score);
break;
case ‘B‘:
updateInfo(studentName, score);
break;
case ‘C‘:
deleteInfo(studentName, score);
break;
case ‘D‘:
queryInfo(studentName, score);
break;
case ‘E‘:
flag = true;
break;
default:
break;
}
if (flag) {
break;
}
}
}
public static void addInfo(String[] studentName, int[] score) {
for (int i = 0; i < studentName.length; i++) {
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
studentName[i] = stName;
System.out.println("请输入学生成绩:");
int scr = scan.nextInt();
score[i] = scr;
}
}
public static void updateInfo(String[] studentName, int[] score) {
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
int index = -1;
for (int i = 0; i < score.length; i++) {
if (stName.equals(studentName[i])) {
index = i;
}
}
System.out.println("请输入学生成绩:");
int scr = scan.nextInt();
score[index] = scr;
}
public static void deleteInfo(String[] studentName, int[] score) {
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
boolean flag = false;
for (int i = 0; i < studentName.length; i++) {
if (studentName.length - 1 == i) {
studentName[i] = null;
score[i] = 0;
break;
}
if (stName.equals(studentName[i]) || flag) {
flag = true;
studentName[i] = studentName[i + 1];
score[i] = score[i + 1];
}
}
for (int i = 0; i < studentName.length; i++) {
System.out.println("学生姓名=" + studentName[i] + ",成绩=" + score[i]);
}
}
public static void queryInfo(String[] studentName, int[] score) {
System.out.println("欢迎进入学生信息查询系统");
System.out.println("=========小菜单=========");
Scanner input = new Scanner(System.in);
System.out.println("A:查询单个学生分数B:查询所有学生分数");
String str = input.next();
char chr = str.toCharArray()[0];
switch (chr) {
case ‘A‘:
System.out.println("请输入学生姓名");
Scanner scan = new Scanner(System.in);
String newsdutic = scan.next();
for (int i = 0; i < studentName.length; i++) {
if (newsdutic.equals(studentName[i])) {
System.out.println("学生" + studentName[i] + "成绩为" + score[i]);
break;
}
}
case ‘B‘:
for (int i = 0; i < studentName.length; i++) {
System.out.println("姓名" + studentName[i] + "成绩" + score[i]);
}
}
}
}
两个程序对比就会知道很多时候函数的重要性。
明天开始学习的东西可能是我在今后学习中会经常运用到的东西,希望可以学懂!
java基础的运用