java基礎的運用

來源:互聯網
上載者:User

標籤:tor   使用者密碼   ext   項目   分數   line   system   代碼   使用者   

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基礎的運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.