public class CreditCardHolder
{
/**
* This method is used to hold credit card numbers
*/
public static void main(String[] args) throws Exception
{
File cardNumbers = new File("creditcardnumbers.txt");
Scanner cardNumbersInput = new Scanner(cardNumbers);
//cardNumbersInput.useDelimiter("| " + System.getProperty("line.separator"));
while(cardNumbersInput.hasNext())
{
String cardNumber = cardNumbersInput.nextLine();
//System.out.println(cardNumber);
if((cardNumber.length() < 18) || (cardNumber.length() > 15))
{
System.out.println(cardNumber + " Is a Mastercard card");
}
else if(cardNumber.length() == 14 )
{
System.out.println(cardNumber + " This is a American Express card");
}
else if((cardNumber.length() > 12) || (cardNumber.length() < 15))
{
System.out.println(cardNumber + " This is a Visa card");
}
}
cardNumbersInput.close();
}
}
我的数字是:
123456789123454个
162626262626262622b
12345678901234
我希望他们全部被分类到他们应该去的地方,并根据字符数打印他们所属的卡片类型,当它不是真的时,它们都会落入第一个if语句中。因为这三个都应该进入所有不同的子类别
答案 0 :(得分:0)
您需要在 IF的条件下使用 AND (&amp;&amp;)运算符而不是 OR (||)运算符语句,例如:
if((cardNumber.length() > 15) && (cardNumber.length() < 18)) {
// Do whatever .................
}