如何在Java中创建条形码

huangapple go评论64阅读模式
英文:

How to create barcode in Java

问题

public String barcode()
{
    String bar = "";
    String alphs = "ABCDFEJKZCVBNMFTYWQRTNMGFDS";
    
    char al[] = alphs.toCharArray();
    
    int n1 = (int) ((Math.random() * 100) % 26);
    int n2 = (int) ((Math.random() * 100) % 26);
    int n3 = (int) ((Math.random() * 100) % 26);
    int n4 = (int) ((Math.random() * 100) % 10);
    int n5 = (int) ((Math.random() * 100) % 10);
    int n6 = (int) ((Math.random() * 100) % 10);       
    bar = al[n1] + "" + al[n2] + "" + al[n3] + "-" + n4 + "" + n5 + "" + n6;  
    return bar;
}

(Note: The provided code seems to generate a random barcode using a combination of letters and numbers.)

英文:

I want to create a barcode in Java. I tried the following code, but barcode created and displayed an error on console:

> Exception in thread "AWT-EventQueue-0"
> java.lang.NumberFormatException: empty String

What I tried so far I attached below

 public String barcode()
    {
        String bar = "";
        String alphs ="ABCDFEJKZCVBNMFTYWQRTNMGFDS";
        
        char al[] = alphs.toCharArray();
        
        int n1 =(int)((Math.random()*100)%26);
        int n2 =(int)((Math.random()*100)%26);
        int n3 =(int)((Math.random()*100)%26);
        int n4 =(int)((Math.random()*100)%10);
        int n5 =(int)((Math.random()*100)%10);
        int n6 =(int)((Math.random()*100)%10);       
        bar = al[n1]+ ""+ al[n2]+""+ al[n3]+"-"+ n4+ ""+ n5+ ""+ n6;  
        return bar;
  
    }

答案1

得分: 0

这段代码在6000000次循环中是正确的。

for (int i = 0; i < 6000000; i++) {
    String barcode = barcode();
    System.out.println(barcode);
}

请检查您的环境。

英文:

This code is correct by 6000000 counts.

for (int i = 0; i &lt;6000000 ; i++) {
String barcode = barcode();
System.out.println(barcode);
}

Please check your environment.

huangapple
  • 本文由 发表于 2020年10月20日 13:53:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64439237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定