Netbeans在我的方法上给了我“非法开始表达”的错误信息

时间:2015-10-02 14:37:10

标签: java netbeans

主要之后的所有方法都得到“非法开始表达”错误消息,我无法弄清楚原因。这是我的代码:

import java.util.Scanner;
public class CreditCardCounter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // This program allows the user to enter multiple credit card numbers, then
        //tallys and displays the number of each different brand of credit card entered.

        // Create a new scanner.
         Scanner input = new Scanner(System.in);
         System.out.print("Enter your Card Number : ");

        long input = sc.nextLong();

        if (isValid(input) == true) {
            System.out.println("\n*****Your card is Valid*****");
        } else {
            System.out.println("\n!!!!Your Card is not Valid !!!!! ");
        }


         public static boolean isValid(long number) {

        int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);


        if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=13 ) && (getSize(number)<=16 )) {
            return true;
        } else {
            return false;
        }
    }

    public static int getDigit(int number) {

        if (number <= 9) {
            return number;
        } else {
            int firstDigit = number % 10;
            int secondDigit = (int) (number / 10);

            return firstDigit + secondDigit;
        }
    }
    public static int sumOfOddPlace(long number) {
        int result = 0;

        while (number > 0) {
            result += (int) (number % 10);
            number = number / 100;
        }

        return result;
    }

    public static int sumOfDoubleEvenPlace(long number) {

        int result = 0;
        long temp = 0;

        while (number > 0) {
            temp = number % 100;
            result += getDigit((int) (temp / 10) * 2);
            number = number / 100;
        }

        return result;
    }

    public static boolean prefixMatched(long number, int d) {

        if ((getPrefix(number, d) == 4)
                || (getPrefix(number, d) == 5)
                || (getPrefix(number, d) == 3)) {

            if (getPrefix(number, d) == 3) {
                System.out.println("\nVisa Card ");
            } else if (getPrefix(number, d) == 5) {
                System.out.println("\nMaster Card ");
            } else if (getPrefix(number, d) == 3) {
                System.out.println("\nAmerican Express Card ");
            }

            return true;

        } else {

            return false;

        }
    }

    public static int getSize(long d) {

        int count = 0;

        while (d > 0) {
            d = d / 10;

            count++;
        }

        return count;

    }

    public static long getPrefix(long number, int k) {

        if (getSize(number) < k) {
            return number;
        } else {

            int size = (int) getSize(number);

            for (int i = 0; i < (size - k); i++) {
                number = number / 10;
            }

            return number;

        }

    }

}

2 个答案:

答案 0 :(得分:2)

你有两个问题妨碍你建立。

首先,input被定义为Scannerlong。对于这样的两个不同的东西,你不能有相同的名字。根据您初始化long input的行,我猜您的Scanner应该被命名为sc

接下来,您的main方法没有右括号。

修复这两个错误似乎可以消除构建错误。但是,你仍然需要发表你的逻辑文本。

答案 1 :(得分:1)

似乎你忘记了一个大括号:

    ...
    if (isValid(input) == true) {
        System.out.println("\n*****Your card is Valid*****");
    } else {
        System.out.println("\n!!!!Your Card is not Valid !!!!! ");
    }
}  // <--- add this bracket