我正在为程序编写代码,我必须在奇数和偶数之间切换。当我尝试执行所有操作时,只重复了一个数字几次。我在做什么错了?
这就是我所拥有的: 套餐creditCard;
导入java.util.Scanner;
公共类Program2CCValidation {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean validCreditCard = false;
int creditCard;
while (!validCreditCard) {
System.out.print("Please enter a credit card number: ");
String potentialCCN = scan.nextLine();
int lastDigit = Integer.parseInt(potentialCCN.substring(potentialCCN.length()-1));
System.out.println("Check credit card number.");
String reversedCCN = "";
for (int i = potentialCCN.length()-1; i >= 0; i--) {
reversedCCN = reversedCCN + potentialCCN.charAt(i);
}
System.out.println(reversedCCN);
boolean isOddDigit = false;
int currentDigit = 0;
int sum = 0;
for (int i = 0; i < reversedCCN.length(); i += 2) {
currentDigit = Integer.parseInt(potentialCCN.substring(potentialCCN.length()-1));
System.out.println(currentDigit);
if (currentDigit + 1 < potentialCCN.length()) {
}
if (isOddDigit) {
currentDigit= currentDigit*2;
}
}
}
}
}
这就是我想要做的:
//set up a Scanner
//set up a boolean named validCreditCard and set it to false
//loop while not a valid credit card
//prompt the user for a potential credit card number
//Get the credit card number as a String - store in potentialCCN (use scanner's nextLine)
//use Luhn to validate credit card using the following steps:
//store the digit AS AN INT for later use in lastDigit (probably requires Integer.parseInt(String)
//TEST then comment out! - check the last digit - System.out.println(lastDigit);
//remove the last digit from potentialCCN and store in potentialCCN using String's substring
//TEST then comment out! - check potentialCCN - System.out.println(potentialCCN);
//create reversedCCN as a empty String
//reverse the digits using a for loop by adding characters to reversedCCN
//TEST then comment out! - check reversedCCN - System.out.println(reversedCCN);
//set boolean isOddDigit to false
//set up an integer called current digit and set it to 0
//create an integer named sum and set it to 0
//multiply the digits in odd positions by 2, then subtract 9 from any number higher than 9 using:
//(for loop running 0 to less than length of reversed CCN)
//set currentDigit equal to the integer version of the current character
//Test then comment out! - currentDigit -System.out.print(currentDigit);
//toggle isOddDigit
//if isOddDigit
//multiply currentDigit by 2 and store in currentDigit
//if currentDigit > 9, subtract 9 from currentDigit and store in currentDigit
//TEST then comment out - Check currentDigit - System.out.println(currentDigit);
}//end if isOddDigit