gpt4 book ai didi

java - 我需要java中的验证方法

转载 作者:行者123 更新时间:2023-12-02 10:14:10 25 4
gpt4 key购买 nike

我需要为学校项目制作自动取款机。我完成了所有工作,一切正常,并且我对引脚进行了验证,因为它是字符串。所以我的问题是如何对所有其他方法进行验证,以检查是否输入了除数字之外的其他内容来表明用户是错误的,并在该方法开始时返回他。所有变量都以整数形式存储到数组中。

所以这是我的代码,请帮助我尝试了很多东西,但我无法使其工作..

public class Banka {

static Scanner skener = new Scanner(System.in);
static String pin[] = {"1234","2345","3456","4567","5678"};
static String username[] = {"Mate","Nathan","John","Michelle","Angelina"};
static int balance[] = {200,100,250,150,300};
static boolean overdraft[] = {true,true,false,true,false};
static int index;

public static void main(String[] args) {
login();
}
public static void login() {
Date datum = new Date();
System.out.println("" +datum);
System.out.println("------------------------------");
System.out.println("Welcome to the Illuminati Bank \n Please log in with your PIN");
String login = skener.next();
checkpin(login);
for (int i = 0; i< pin.length; i++) {
if (login.matches(pin[i])) {
index = i;
System.out.println("\nWelcome " + username[index]);
Menu();
}
}
System.out.println("Wrong PIN entered, please login again \n");
login();
}
public static void Menu() {
System.out.println("Please select an option");
System.out.println("\n 1.View Bank Statement \n 2.Deposit \n 3.Withdraw \n 4.Change Pin \n 5.Exit \n");
int choice = skener.nextInt();

switch (choice) {
case 1: statement();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: change();
break;
case 5: exit();
break;
default: System.out.println("Incorrect Choice ");
Menu();
}
}
public static void statement() {
switch(index) {
case 0: case 1: case 2: case 3: case 4:
System.out.println("" +username[index]+ ", your balance is: " +balance[index]+ "€");
if (overdraft[index] == true) {
System.out.println("You are entitled to overdraft");
}
else {
System.out.println("You are NOT entitled to overdraft");
}
Menu();
}
}
public static void deposit() {
System.out.println("" +username[index]+ ", how much you wish to deposit?");
int deposit = skener.nextInt();
balance[index] = balance[index] + deposit;
System.out.println("Thank you, you deposited " +deposit+ "€, now you have " +balance[index]+ "€ total");
depositm();
}
public static void depositm(){
System.out.println("\n 1.Deposit more \n 2.Exit to menu");
int more = skener.nextInt();
switch (more) {
case 1: deposit();
break;
case 2: Menu();
default: System.out.println("Wrong choice, please choose again");
depositm();
}
}
public static void withdraw() {
System.out.println("" +username[index]+ ", how much you wish to withdraw?");
int withdraw = skener.nextInt();
if (overdraft[index] == true) {
balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
if(overdraft[index] == false && balance[index] >= withdraw)
{balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
else {
System.out.println("You have insufficient funds \nPlease try again");
withdraw();
}
}
public static void change() {
System.out.println("" +username[index]+ ", do you want to change your pin?");
System.out.println("Press 1 to change or 2 to exit to menu");
int change = skener.nextInt();
switch (change) {
case 1: System.out.println("Please enter new PIN");
pin[index] = skener.next();
System.out.println("You successfully changed your PIN");
Menu();
case 2: System.out.println("Your PIN remains unchanged");
Menu();
default: System.out.println("Wrong choice, please choose again");
change();
}
}
public static void exit(){
System.out.println("Goodbye " +username[index]+ ", Illuminati Bank wish you all the best \n");
login();
}
public static int checkpin(String x){
while(!x.matches("\\d{4}")){
System.err.println("\n Error.\n Please enter 4 digit pin.");
login();
}
return 0;
}
}

因此,如果有人可以帮助我如何使用用户输入验证所有其他方法,那么 INT 在哪里,那就太好了。

最佳答案

试试这个

String input = "xxxx";
int pin;
try{
pin = Integer.parseInt(input);
}catch(NumberFormatException e){
// input contains letters or symbols.
}
<小时/>

或者这是另一个使用 Character 的例子类。

String input = "xxxx";
boolean allDigits = true;
for(char ch: input.toCharArray()){
if(!Character.isDigit(ch)) {
allDigits = false;
break;
}
}

if(allDigits){
// input contains only digits.
}
<小时/>

编辑:回答this评论。

你可以像这样修改你的方法,

public static void checkpin(String x) {
if (x.length() == 4) {
try {
Integer.parseInt(x);
login();
} catch (NumberFormatException e) {
System.err.println("\n Error.\n Invalid pin.");
}
} else {
System.err.println("\n Error.\n Please enter 4 digit pin.");
}
}

这样,仅当 PIN 码有 4 位且所有数字均为数字时,才会调用 login() 方法。

关于java - 我需要java中的验证方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54789918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com