我已经完成了这个项目,我的java编程类介绍。这是一个信用卡验证程序。我知道我已经非常接近完成了。我需要做的就是询问用户是否要在第一张卡号后输入另一张卡号。 通常程序说请输入您的卡号 用户输入卡号 程序确定签证,万事达卡等......然后确定是否有效。 很简单。但我需要改变它来说
“请输入卡号”
用户输入卡号
“你想输入另一张卡号吗?”
用户输入是或否
如果是,则该方法重新运行并获得另一个号码,然后继续执行此操作,直到用户拒绝为止。 在用户拒绝之后,程序会确定这些卡是签证,万事达卡等......以及它们是否有效。
我的代码发布在下面。我很确定我需要一个while循环,但我不太确定如何设置它。下面的代码只是getcardnumber方法。
TLDR; 在用户停止输入卡号之前,如何重复此操作?
public static String getCardNumber() {
String cardNumber;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the credit card number: ");
cardNumber = keyboard.nextLine();
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
while (!isAllDigits(cardNumber)) {
System.out.println("The credit card number must contain only the"
+ " digits 0-9; please re-enter: ");
cardNumber = keyboard.nextLine();
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
}
//a number containing only digits has been entered
return cardNumber;
}
答案 0 :(得分:0)
您可以这样做:
public static void main(String[] args){
boolean b = false;
do {
System.out.println("Number 2: ");
Item2 item2 = new Item2();
System.out.println("Please enter the credit card number: ");
Scanner keyboard = new Scanner(System.in);
System.out.println(checkNumber(keyboard.nextLine(), keyboard));
System.out.println("Would you like to input another card number? 1=Yes; 2=No");
int decision = Input.nextInt();
if (decision == 1){
b = false;
}else if (decision == 2){
b = true;
}
}while(b == false);
}
public static String checkNumber(String cardNumber, Scanner keyboard) {
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
while (!isAllDigits(cardNumber)) {
System.out.println("The credit card number must contain only the"
+ " digits 0-9; please re-enter: ");
cardNumber = keyboard.nextLine();
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
}
//a number containing only digits has been entered
return cardNumber;
}