以下是用于将输入的卡号的数字相加的代码:
import java.util.Scanner;
import java.lang.*;
import java.math.*;
public class Assignment4
{
public static void main(String[] args)
{
// introduce program
System.out.println("\n========================");
System.out.println("Card Number Verification");
System.out.println("========================\n");
Scanner sc = new Scanner(System.in); // initialize scanner
// intialize any relevant variables
String card_type = "0"; // for Visa or MasterCard in part 1
int sum = 0; // sum for all digits in card number in part 2
int num_check; // for modulo 10 of summation of digits in part 3
/* Part 1: Determine users card type */
System.out.println("Menu (choose number):");
System.out.println("---------------------");
System.out.println("1 - MasterCard");
System.out.println("2 - Visa\n");
int menu_choice = sc.nextInt();
sc.nextLine();
switch (menu_choice) {
case 1:
card_type = "MasterCard";
break;
case 2:
card_type = "Visa";
break;
default:
System.out.print("Not a menu option. Restart the program ");
System.out.println("and choose 1 or 2.\n");
System.exit(0);
}
/* Part 2: Obtain users card number and sum digits */
System.out.print("\nEnter 16 digit card number (spaces every ");
System.out.println("4 digits): ");
String card_string = sc.nextLine(); //obtain users card number
int length = card_string.length(); // get length of number
// verify that it is 16 numeric characters. '\\d{$} ' verifies
if (card_string.matches("(\\d{4} \\d{4} \\d{4} \\d{4}") == false) {
System.out.print("\nNot a valid card number. Restart the program ");
System.out.println("and enter a 16 digit number.\n");
System.exit(0);
}
for (int i=0; i < length;) {
// the following sum adds the value of the digit at point i
if (i == 4 || i == 8 || i == 12) {
i++;
else {
sum += Character.getNumericValue(card_string.charAt(i));
i++;
//System.out.println(sum); // used for testing
}
}
/* Part 3: Check validity of users card number */
num_check = sum % 10; // modulo 10 of summation of digits
if (card_type == "MasterCard") {
if (num_check == 1) {
System.out.println("\nValid MasterCard number!\n");
}
else {
System.out.println("\nInvalid MasterCard number.\n");
}
}
else {
if (num_check == 0) {
System.out.println("\nValid Visa card number!\n");
}
else {
System.out.println("\nInvalid Visa card number!\n");
}
}
System.exit(0);
}
}
这是它产生的错误:
Assignment4.java:60: 'else' without 'if'
else {
^
Assignment4.java:88: reached end of file while parsing
}
^
2 errors
Jordans-MacBook-Pro:CSE_110_hw JPagz95$ javac Assignment4.java
Assignment4.java:59: 'else' without 'if'
else {
^
Assignment4.java:87: reached end of file while parsing
}
^
2 errors
有人可以解释为什么它不承认我有if语句?当我更改if语句以识别何时i是某个值(4,8,12不求和)时,问题就开始了。一切都有帮助。我会在一两个小时(晚餐时间)给出最佳答案:)
答案 0 :(得分:1)
您的if / else定位错误。应该是
if (i == 4 || i == 8 || i == 12) {
i++;
}
else {
sum += Character.getNumericValue(card_string.charAt(i));
i++;
//System.out.println(sum); // used for testing
}
答案 1 :(得分:1)
你的if块只是错过了它的结束括号:)
if (i == 4 || i == 8 || i == 12) {
i++;
}
else {
sum += Character.getNumericValue(card_string.charAt(i));
i++;
//System.out.println(sum); // used for testing
}