问题描述
公共课程序{
// Global variables to be use in the whole program
public static int numOfInvalids = 0,visaNum=0 ;
public static void main(String[] args)
{
Scanner userInput = new Scanner(system.in) ;
System.out.println("How many credit card you want to check?");
int numOfCredit = userInput.nextInt() ;
int creditnumbers[] = new int[numOfCredit] ;
for (int i = 0 ; i<numOfCredit ; i++)
{
System.out.println("Enter credit card number "+(i+1)+": ") ;
String creditNumber = userInput.next() ;
// checks if number is a credit number or not
if (creditNumber.length()<16 || creditNumber.length()>19)
{
System.out.println("Not a credit card number!") ;
prog.numOfInvalids ++ ; // count number of invalid card numbers
System.out.println();
continue ;
}
checkValid(creditNumber);
checkType(creditNumber) ;
// calls function
}
System.out.println("Number of invalid: "+prog.numOfInvalids) ;
System.out.println("Number of visa credit card: "+prog.visaNum) ;
}
private static boolean checkValid(String cardNumber) // function to check if card number is valid or not
{
int cardlength = cardNumber.length();
int evenSum = 0,oddSum = 0,sum;
for (int i = cardlength - 1; i >= 0; i--) //looping from the right
{
int digit = Character.getNumericValue(cardNumber.charat(i)); // Character.getNumericValue = convert char to integer
if (i % 2 == 0) // taking every 2nd digits from the right e.g 2nd..4th..6th..8th etc.
{
int multiplyByTwo = digit * 2; // multiply each 2nd digits by 2
if (multiplyByTwo > 9) // Add two digits to handle cases that make two digits after doubling e.g (6 * 2 = 12) = 1 + 2
{
String doubleDigits = String.valueOf(multiplyByTwo); // convert double digit to String for use of charat function
multiplyByTwo = Character.getNumericValue(doubleDigits.charat(0)) + Character.getNumericValue(doubleDigits.charat(1)); //convert each character to integer and add them together
}
evenSum += multiplyByTwo; // add product digits together
}
else
{
oddSum += digit; // Add sum of digits that weren’t multiplied by 2
}
}
sum = evenSum + oddSum; //Adding both sums together
if (sum % 10 == 0) //if sum is multiple of 10,it is valid
{
System.out.println("valid card");
System.out.println();
return true;
}
else
{
System.out.println("invalid card") ;
prog.numOfInvalids ++ ; // count the number of invalid card numbers
System.out.println();
return false;
}
}
private static boolean checkType(String cardNumber) // function to check card type
{
if ( (cardNumber.charat(0)=='4') && cardNumber.length()==13 || cardNumber.length()==16 ) //checking first character and length of card number
{
System.out.println("VISA");
visaNum ++ ; // counting number of visa cards
return true;
}
else
{
return false;
}
} }
我只想从第一个函数“check Valid”到最后一个函数“Check Type”检查有效卡的卡类型
有什么办法可以保存第一个函数中的有效数字并在“检查类型”函数中使用它们?
我只想从第一个函数“check Valid”到最后一个函数“Check Type”检查有效卡的卡类型
有什么办法可以保存第一个函数中的有效数字并在“检查类型”函数中使用它们?
解决方法
基于问题的假设
- 只需要为有效卡推断卡类型
- 无法更改函数返回类型
可能的方法
- 对
if
的响应添加checkValid
检查,如果为真,则调用checkType
if (checkValid(creditNumber) {
checkType(creditNumber);
}
建议
- 无论采用哪种解决方案,请在编码和提问时改进格式,因为这将有助于其他人更好地理解问题。