Как переключаться между четными и нечетными числами в строке?

#java #string

#java #строка

Вопрос:

Я пишу код для программы, и мне нужно переключаться между четными и нечетными числами. Когда я попытался это сделать, все, что он сделал, это просто повторил одно число несколько раз. Что я делаю не так?

Это то, что у меня есть: пакет CreditCard;

импортируйте java.util.Сканер;

открытый класс 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
  

Комментарии:

1. Где «переключение»? Вы установили isOddDigit значение false и никогда его не меняли. Это то, что вы спрашиваете: «Как мне изменить isOddDigit с false на true или с true на false для каждой цифры?»