gpt4 book ai didi

java - 获取信用卡号并输出公司名称

转载 作者:行者123 更新时间:2023-12-02 05:19:48 24 4
gpt4 key购买 nike

需要获取发卡行和号码,仅输出“未知”。(底部只是测试代码)。我是否试图测试错误的变量,我是否错误地使用了indexOf()?如有任何帮助,我们将不胜感激

public class CreditCard 
{
private String card_number;
private boolean is_number;
private String number_string = "";
public String issuer_name = "";

public CreditCard(String card_number)
{
this.card_number = card_number;
}

public String toString()
{
for (int x = 0; x < card_number.length(); x++)
{
char y = card_number.charAt(x);
is_number = Character.isDigit(y);
if (is_number)
{
number_string += y;
}

}

String s = number_string + " was issued by " + getIssuer();
return s;
}

public void setIssuer(String issuer)
{
issuer_name = issuer;
}

public String getIssuer()
{
String issuer_Name;
if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16)
{
issuer_Name = "VISA";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('8') == 1 && card_number.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('4') == 1 && card_number.length() == 15)
{
issuer_Name = "American Express";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('7') == 1 && card_number.length() == 15)
{
issuer_Name = "American Express";
}
if (card_number.indexOf('5') == 0 && card_number.indexOf('1') == 1 && card_number.length() == 16)
{
issuer_Name = "MasterCard";
}
if (card_number.indexOf('5') == 0 && card_number.indexOf('5') == 1 && card_number.length() == 16)
{
issuer_Name = "MasterCard";
}
if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
{
issuer_Name = "Discover";
}
else
{
issuer_Name = "Unknown";
}
return issuer_Name;
}

public static void main(String[] args)
{
System.out.println(new CreditCard("42225-22222222"));
System.out.println(new CreditCard("76009644571"));
System.out.println(new CreditCard("50197170-10103742"));
System.out.println(new CreditCard("6331101899890016"));
}


}

最佳答案

if else 子句会开始类似

if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16) {
issuer_Name = "VISA";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14) {
issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14) {
issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14) {
issuer_Name = "Diner's Club";
} else {
issuer_Name = "Other";
}

注意,我不是在考虑算法的正确性,而是您询问如何执行 if/else

关于java - 获取信用卡号并输出公司名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599186/

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