x
.PNG
Download PNG • 19KB
Finish this question for homework. Assume that the length of the credit card will always be 10 digits long("1234567890"). This is the link for the recordings https://drive.google.com/drive/folders/1AfzT7jDvoVIlyG6EslVcjnS7QdyihlvO?usp=sharing.
This is the link for the slides I used: https://drive.google.com/file/d/1Lf4grCiLBGUlEbJK8hay0RFy4tElXleH/view.
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter your credit card number."); String creditcardnumber = sc.nextLine(); //System.out.println(creditcardnumber); int r1=0; int r2=0; int r3=0; for(int counter=0; counter<creditcardnumber.length(); counter++){ //Sum of all digitals char ch= creditcardnumber.charAt(counter); int currentInt = Integer.parseInt(String.valueOf(ch)); r1= r1+ currentInt; System.out.println("r1="+r1); //Sum of 2nd digits if(counter%2==1){ r2 = currentInt + r2; System.out.println("r2="+ r2); //Number of 2nd dights that are greater than 4 if (currentInt>4) { r3 = r3 + 1; System.out.println("r3="+r3); } } } int r4=0; r4 = r1 + r2 + r3; if (r4%10==0) { System.out.println("Credit Card Valid"); } else System.out.println("Credit Card Invalid"); } }