java.util.Scanner類,這是一個用于掃描輸入文本的新的實用程序。其中nextInt()獲取String型,而next()獲取int、double型。
這是一個仿ATM的小程序。
實現條件 1.登陸界面,2.三次登陸機會,登陸成功進入登陸菜單,3,進入菜單進行選擇,實現 1,查詢,2,存款,3,取款,4,修改密碼,5退出,再位選擇退出時可一直重復操作。
import java.util.Scanner;
public class TestATM{public static void main(String[] args) {String user = "admin";String password ="123456";double money = 10000;Scanner sc = new Scanner(System.in);welcome();boolean b = login(user,password);/*login successful come to the menu*/while(b==true){/System.out.println("login successful !");System.out.println("Now You In The Menu And Select Any Operation.");System.out.println("***1.Check the Balance***"+"\n"+ "***2.Withdrawing Money***"+"\n"+ "***3.Deposit Money***"+"\n"+ "***4.Change Password***"+"\n"+ "***5.exit***");while(true){int opt = sc.nextInt();switch (opt){case 1:checkMoney(money);retreate();break;case 2:money = withdrawMoney(money);System.out.println("operate successfully,you remaining sum is "+money);retreate();break;case 3:money = depositMoney(money);System.out.println("operate successfully,you remaining sum is "+money);retreate();break;case 4:password = changePassword(password);retreate();break;case 5:retreate();System.exit(0);default:break;}}}}public static void welcome(){System.out.println("***********************************************");System.out.println("--------------------------Welcoming------------");System.out.println("--------------------------Version 1 ------------");System.out.println("-----------------------------------------------");}/*Three times to verify login*/public static boolean login(String us,String pw){System.out.println("***Only Thirds Tiems Please Carefully***");int k = 2;for(int i = 1; i<4;i++){ Scanner sc = new Scanner(System.in);System.out.println("please input your username:");String user_name = sc.next();System.out.println("please input your password:");String user_pw = sc.next();if (us.equals(user_name)&&pw.equals(user_pw))return true;else{ if(k!=0){System.out.println("***Please Again Input Your Name And Password"+" "+"Only"+" "+k-- +" "+"Times***");}elseSystem.out.println("You Have No Time,Please Contact Our Staff Now.");continue;}}return false;}/*check money*/public static void checkMoney(double money){System.out.println("Your Balance is"+" "+money+" RMB ");}public static double withdrawMoney(double money1){Scanner sc =new Scanner(System.in);System.out.println("please input you want to withdraw money ");double wm = sc.nextInt();money1 = money1 - wm;return money1;}public static double depositMoney(double money2){Scanner sc =new Scanner(System.in);System.out.println("please input you want to set money ");double sm = sc.nextInt();money2 = money2 + sm;return money2;}public static String changePassword(String old_password){System.out.println("please input your old ps");Scanner sc = new Scanner(System.in);String pd = sc.next();if(pd.equals(old_password)){ System.out.println("please input your new pw ");String pw1 = sc.next();System.out.println("please again input your new pw");String pw2 = sc.next();if (pw1.equals(pw2)) {System.out.println("chage password is successful.");return pw1;}else{System.out.println("The two passwords differ.");return pd;}}else{System.out.println("input old password is failed try again.");return old_password;}}public static void retreate(){System.out.println("Continue OR Not Operation"+"(y/n)");Scanner sc = new Scanner(System.in);String cn = sc.next();if (cn.equals("y")) {return ;}else if (cn.equals("n")) {System.out.println("Exit successful!1");System.exit(0);}else {while (true) {System.out.println("input is failed again?(y/n))");String temp = sc.next();if (temp.equals("y")) {return;} else if (temp.equals("n")) {System.out.println("Exit successful!2");System.exit(0);}}}}
}