信用卡验证器混淆

时间:2015-03-04 01:44:27

标签: java

我很难尝试了解下一步该做什么。到目前为止,我已经将代码更正了。我的指示是创建一个没有阵列的信用卡验证器,我们是一个初学的计算机科学课程,但我们还没有达到这一点。这是我的代码。每当我编译代码时,它都会给我错误:

" java:21:错误:类CreditCardValidator中的方法isValid无法应用于给定类型; if(isValid(card)){    required:int,int,String    发现:字符串    原因:实际和正式的参数列表长度不同

/**
   Credit Card Validator

   @author  Your Name
   @date    Today's Date
   @class   Our Class
*/
import java.util.Scanner;
public class CreditCardValidator {

   public static void main (String[] args) {
      String card = "4012888888881881";

      // When you are finished writing the methods below,
      // uncomment the three lines below to test.

      // Scanner input = new Scanner(System.in);
      // System.out.print("Enter a Credit Card Number: ");
      // String card = input.nextLine();

     if (isValid(card) ) {
         System.out.print("valid");
      } else {
         System.out.print("invalid");
         // If False, state the card number is invalid
     }
      // System.out.println(getDigit(18) + " should be 9 ");
      // System.out.println(getDigit(5) + " should be 5 ");
      //System.out.println(sumOfDoubleEvenPlace(card+ "should be 47"));
   }

   /** 
      Returns true if the card number is valid 

      To determine if a card is valid, the sum of the Double Even Place
      Numbers and the Sum of the Odd Place Numbers must be divisible by
      ten (10), the String must be 13 to 16 digits, *and* the String must
      start with "4", "5", "37", or "6".

      @param number: A 13 to 16 digit String of numbers

      @returns true if the String is a valid card, False otherwise
   */
   public static boolean isValid(int totalEven, int totalOdd, String company) {
        if (((totalEven + totalOdd) % 10 == 0) && company.equals("Visa")) {
          return true;
        } else {
            if (((totalEven + totalOdd) % 10 == 0) && company.equals("Master Card")) {
                return true;
            } else {
                if (((totalEven + totalOdd) % 10 == 0) && company.equals("American Express")) {
                    return true;
                } else {
                    if (((totalEven + totalOdd) % 10 == 0) && company.equals("Discover Card")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }   
        }
   }    
   /** 
      Double every second digit from *right to left*. 

      If doubling of a digit results in a two-digit number, add the two digits
      together to get a single digit number using the getDigit(...) method.

      Use a *loop* to cycle through all the numbers of the String.
      Note: You will need to *convert a char to an int*

      @param number: A 13 to 16 digit String of numbers

      @returns an integer
   */
   public static int sumOfDoubleEvenPlace(String number){
        int totalEven = 0;
        int start = number.length() - 1; // starts at 14
        // i = 14;  14 > 0;  14 -= 2
        for (int i = start; i >= 0; i -= 2) {
            char c = number.charAt(i);
            int digit = Character.getNumericValue(c) * 2;
            // System.out.println(digit + " " + getDigit(digit));
            totalEven += getDigit(digit);
        }
    return totalEven; // Total of double even place
   }
   /**  
      Return this number if it is a single digit, otherwise, return the sum 
      of the two digits. For example, 18 will return 9 (because 1 + 8)
      @param number: a digit that will be between 0 and 18
      @returns an integer
   */
   public static int getDigit(int number){
     int calc = 0;
     if (number < 10) {
    // This is one digit
   } else {
       // This is two digits
        calc = number - 9;
   }
   return calc;
   }
   /** 
      Return sum of odd-place digits in number

      Use a *loop* to cycle through all the numbers of the String.
      Note: You will need to *convert a char to an int*

      @param number: A 13 to 16 digit String of numbers

      @returns an integer
   */
   public static int sumOfOddPlace(String number){
     int totalOdd = 0;
     int start = number.length();

     for (int i = start; i >= 0; i -= 2) {
         char c = number.charAt(i);
         int digit = Character.getNumericValue(c) * 2;

        totalOdd += getDigit(digit);
     }
    return totalOdd;
   }

   /** 
      Return the company of the card by looking at the character
      at the zero (0) index of number.

      @param number: A 13 to 16 digit String of numbers

      @returns a String that is either 
         "Visa", "Master Card", "American Express", or "Discover Card"
   */
   public static String getCompany(String number){
     int card = number.length();
     int i = card - number.length();
     char c = number.charAt(i);
     int digit = Character.getNumericValue(c);
     int d = i + 1;
    String company = "";

    if (digit == 4 ) {
      company = "Visa";
   } else if (digit == 5) {
       company = "Master Card";
   } else if (digit == 6) {
       company = "Discover Card";
   } else if (digit == 3 && d == 7) {
       company = "American Express";
   }
        return company;
   }
}

1 个答案:

答案 0 :(得分:0)

方法签名是 public static boolean isValid(int totalEven, int totalOdd, String company)

在第21行,您正在使用它 isValid(card)其中card是一个字符串,“4012888888881881”

错误告诉您到底出了什么问题。在没有详细查看的情况下,我怀疑你首先需要调用sumOfOddPlace()和sumOfDoubleEvenPlace()并使用那些调用isValid()的结果

如果您使用的是Eclipse或IntelliJ等IDE,则应在编辑器中将其标记为