-2
package homework4;

public class CreditCardNumber {
private String issuerID = "000000";
private String accountNum = "999999999";
private int checkDigit = 0;

public CreditCardNumber(String TempissuerID, String TempaccountNum)
{
    if(TempissuerID != null && TempaccountNum != null && TempissuerID.length() == 6 && TempaccountNum.length() == 9)
        if(Digits(TempissuerID) && Digits(TempaccountNum))
        {
            issuerID = TempissuerID;
            accountNum = TempaccountNum;
            calcCheckDigits();
        }

}

public boolean Digits(String temp1)
{
    String temp = "0123456789";
    int count = 0;
    for(int i = 0; i < temp1.length();i++)
        for(int j = 0; j < temp.length();j++)
            if(temp1.charAt(i) != temp.charAt(j))
            {
                count++;

            }
    if(count == temp1.length()){
        return true;
    }
    return false;
}

public CreditCardNumber(){}

public String getID()
{
    return issuerID;
}

public String getAccNum()
{
    return accountNum;
}

public int getDigits()
{
    return checkDigit;
}

private void calcCheckDigits()
{
    int sum;
    sum = checkSum();
    if((sum + checkDigit) % 10 != 0)    
    {
        checkDigit = sum - (sum % 10);
    }

}

public void CreateCred(String TempissuerID)
{
    if(TempissuerID != null && TempissuerID.length() ==6 && Digits(TempissuerID))
    {
        issuerID = TempissuerID;
    }
    else 
    {
        issuerID = "000000";
    }
    StringBuilder TempString = new StringBuilder();
    for(int i = 0; i < 9 ; i++)
    {
        TempString = TempString.append((Math.random()*(9-0+1)+0));
        System.out.printf("%d",TempString);
    }
    accountNum = TempString.toString();
    calcCheckDigits();
}

 private int checkSum()
 {       StringBuilder temp = new StringBuilder();
         int num;
         int sum =0;
         for(int i = 0 ; i <issuerID.length();i++)
         {
                 temp.append(issuerID.length());
                 for(int j = 0 ; j < accountNum.length(); j++)

                 temp.append(accountNum.length());
         }
         for(int k = 0 ; k < temp.length(); k +=2)
         {
                 num = temp.charAt(k) - '0';
                 num *=2;
                 if(num > 9)
                   num = 1 + (num % 10);
                 temp.setCharAt(k, (char) num);
         }
         for(int v = 0 ; v < temp.length(); v++)
         {
                 sum += temp.charAt(v) - '0';
         }

         return sum;
 }

 public String toString()
 {      
     return issuerID + accountNum + checkDigit;

 }
}

// 在另一个文件中

package homework4;
import java.util.Scanner;
public class Prog4 {

public static void main(String[] args)
{   CreditCardNumber[] cred1;
    CreditCardNumber cred2 = getInput();
    Display(cred2);
    cred1 = getInputArray();
}

public static CreditCardNumber getInput() {
    String ID;
    String accNum;
    CreditCardNumber tempCred;      
    Scanner scanner = new Scanner(System.in);
    System.out.printf("Please enter issuer ID:");
    ID = scanner.next();
    System.out.printf("Please enter account number:");
    accNum = scanner.next();
    tempCred = new CreditCardNumber(ID, accNum);
    return tempCred;
}
public static void Display(CreditCardNumber cred2)
{

    System.out.printf("The complete number from your input:");
    System.out.println(cred2);
    return;

}

public static CreditCardNumber[] getInputArray()
{
    CreditCardNumber[] tempArray;
    String tempID;
    int size;
    Scanner scanner = new Scanner(System.in);
    System.out.printf("Please enter size of the aray:");
    size = scanner.nextInt();
    if(size < 1)
    {
        size = 1;
    }
    tempArray = new CreditCardNumber[size];
    System.out.printf("Please enter issuer ID:");
    tempID = scanner.next();
    for(int i = 0; i < size; i++)
    {

    }

    return tempArray;
}

}

嗨,我对 main 中的 getInput 方法有疑问,当我编译代码并运行它时,输出是

enter issuer ID:321321
Please enter account number:654654654
The complete number from your input:0000009999999990 

这是 CreditCardNumber 类中 issuerId 和 acountNum 的默认值

但我想要的是这个输出

Enter a credit card issuer number: 321321
Enter an account number: 654654654
The complete number from your input: 
3213 2165 4654 6549

每个 4 个字符之间有 4 个空格,为什么我的代码输出默认值而不是用户输入值?

谁能告诉我代码或 getinput() 函数有什么问题?

预先感谢

4

2 回答 2

0

if(temp1.charAt(i) != temp.charAt(j))应该if(temp1.charAt(i) == temp.charAt(j))public boolean Digits(String).

您将字符列入白名单,因此您应该计算匹配的字符,而不是匹配的字符。

另一种验证字符串是否由至少一个数字组成的方法:

public boolean Digits(String str) {
    for(int i = 0; i < str.length(); i++)
        if(str.charAt(i) < '0' || str.charAt(i) > '9')
            return false;
    return str.length() > 0;
}

没有测试过这个,但它应该可以工作。

于 2012-11-17T01:22:16.737 回答
0

我刚刚调试了您的代码,您的 digit 方法为此特定输入返回 false ,这将使构造函数中的 if 条件失败,因此它仍将保留默认值。

只需使用正则表达式来检查传递的字符串是否为数字。

更改您的 Digits 方法,如下所示。

    public boolean Digits(String temp1)
    {
        if(temp1.matches("[0-9]+")){
            return true;
        }
        else {
            return false;
        }
}

像这样改变你的 toString 方法:

 public String toString()
 {   String s="";   
     String str= issuerID + accountNum ;

     return str;

 }

它给了我输出:

Please enter issuer ID:321321
Please enter account number:654654654
The complete number from your input:321321654654654
于 2012-11-17T01:31:53.697 回答