-2
試圖編寫一個程序,該程序採用奇數索引處的所有數字並且應該被添加以形成總和。 實施例:如果信用卡號碼是43589795,則形成的總和3 + 8 + 7 + 5是23.從用戶輸入的號碼中抓取單個號碼並檢查它是奇數還是偶數索引
然後雙每個是在偶數索引,並添加所得的所有數字數字數字的總和。 例如:上面的數字,加倍數字產生8 10 18 18.添加這些值中的所有數字產生8 + 1 + 0 + 1 + 8 + 1 + 8爲27,所以最終總和爲23 + 27如果最後一筆金額的最後一位是0,那麼信用卡號碼是有效的。在我們的例子中,最後一位50是0,所以這個數字是有效的。
我將不勝感激一些幫助,我試圖更好地理解循環,並使用Character.isDigit。謝謝!
import java.util.Scanner;
public class CreditCard {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
final String CREDIT_CARD = "[0-9]{8}";
int length = 8;
String num, response;
char ch1, ch2;
do {
do {
System.out.println("Please enter a 8 digit credit cardnumber");//validating that the user entered 8 digits.
num = scan.next();
ch1 = num.charAt(0);
} while (!num.matches(CREDIT_CARD));
for (int i = 0; i < CREDIT_CARD.length(); i++){ //for loop for checking if number is at odd or even index?
Character.isDigit(ch1); //Trying to use Character.isDigit to validate whether it is at odd or even index?
if (ch1 % 2== 1){
}
}
System.out.println("Would you like to enter a different card?");
response = scan.next();
}while (response.equals("yes")); //Do while loop asking if user wants to enter another card.
System.out.println("Goodbye!");
}
}
花花公子你真的不熟嗎?沒有提供原因的向下投票? – cody123