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() 函数有什么问题?
预先感谢