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()函数有什么问题吗?
提前谢谢
答案 0 :(得分: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;
}
没有对此进行测试,但它应该有效。
答案 1 :(得分:0)
我刚刚调试了你的代码,你的digit方法对于这个特定的输入返回false,这将使构造函数中的if条件失败,因此它仍然保持默认值。
只需使用正则表达式来检查传递的字符串是否为数字。
更改您的数字方法,如下所示。
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