如何在java中为public static int方法创建主类?

时间:2014-08-25 19:43:20

标签: java

我很抱歉这个noob问题,但我并不熟悉这种方法。此方法为ean 8条形码生成校验位。如何为此方法创建主类?有没有其他方法可以为ean 8条形码生成校验位?

public class CheckDigit { 
   public static int checkdigit(String idWithoutCheckdigit) {
      String validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ_";
      idWithoutCheckdigit = idWithoutCheckdigit.trim().toUpperCase();
      int sum = 0;

      for (int i = 0; i < idWithoutCheckdigit.length(); i++) {
         char ch = idWithoutCheckdigit.charAt(idWithoutCheckdigit.length() - i - 1);


         if (validChars.indexOf(ch) == -1)
            throw new RuntimeException("\"" + ch + "\" is an invalid character");

         int digit = ch - 48;
         int weight;

         if (i % 2 == 0) {
            weight = (2 * digit) - (digit / 5) * 9;
         } else {
            weight = digit;
         }
         sum += weight;
      }

      sum = Math.abs(sum) + 10;
      return (10 - (sum % 10)) % 10;

   }
}

4 个答案:

答案 0 :(得分:0)

只需在班级中添加其他方法:

public class CheckDigit {
    public static int checkdigit(String idWithoutCheckdigit) {
        /*
            Good looking implementation of your method
        */
    }

    public static void main(String[] args) {
        //fill the String with a expected value
        String idWithoutCheckdigit = "...";
        //evaluate the String with your method
        int useAGoodNameForThisVar = checkdigit(idWithoutCheckdigit);
        //print the results
        System.out.println("checkdigit(" + idWithoutCheckdigit + ") = " + useAGoodNameForThisVar);
    }
}

答案 1 :(得分:0)

java中的主要方法看起来像这样(有一个main方法使类成为主类)。

public static void main(String[] args)
{
     //code...
}

也许那时你想要调用你的方法

public static void main(String[] args)
{
     // print the results of your method
     System.out.println(checkdigit("This is the string i'm giving to my method."));
}

答案 2 :(得分:0)

像这样:

public class CheckDigit {

    public static void main(String[] args) {
        if (args.length > 0) {
            for (String arg : args) {
                System.out.println(CheckDigit.checkdigit(arg));
            }
        } else {
            System.out.println("Please give some values on the command line");
        }
    }

    public static int checkdigit(String idWithoutCheckdigit) {
        String validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ_";
        idWithoutCheckdigit = idWithoutCheckdigit.trim().toUpperCase();
        int sum = 0;
        for (int i = 0; i < idWithoutCheckdigit.length(); i++) {
            char ch = idWithoutCheckdigit.charAt(idWithoutCheckdigit.length() - i - 1);
            if (validChars.indexOf(ch) == -1)
                throw new RuntimeException("\"" + ch + "\" is an invalid character");
            int digit = ch - 48;
            int weight;
            if (i % 2 == 0) {
                weight = (2 * digit) - (digit / 5) * 9;
            } else {
                weight = digit;
            }
            sum += weight;
        }
        sum = Math.abs(sum) + 10;
        return (10 - (sum % 10)) % 10;
    }
}

答案 3 :(得分:0)

public static void main(String[] args){
    for(String each : args){
        System.out.println(CheckDigit.checkdigit(each));
    }
}