切换案例错误...代码没有返回任何内容

时间:2016-10-18 04:42:15

标签: java barcode zipcode

我正在处理的项目的分配是用户能够输入邮政编码和程序在输出中提供条形码。例如,95014的条形码现在应该是" |:| ::: |:|:|| :::::: ||:| :: |"。这完全基于我所拥有的表,表示每个数字应该是什么(显示在case语句中)。这是我为该部分编写的代码:

public class Zipcode{

private String zipcode = "";
private String barcode = "";
private int zipnum = 0;

public Zipcode(int zip_number){
    zipnum = zip_number;
}

public void createBarcode(){
    while (zipnum > 0){
        switch (zipnum % 10)
        {
            case 0: 
                barcode = "||:::";
                break;
            case 1: 
                barcode = ":::||";
                break;
            case 2: 
                barcode = "::|:|";
                break;
            case 3: 
                barcode = "::||:";
                break;
            case 4: 
                barcode = ":|::|";
                break;
            case 5: 
                barcode = ":|:|:";
                break;
            case 6: 
                barcode = ":||::";
                break;
            case 7: 
                barcode = "|:::|";
                break;
            case 8: 
                barcode = "|::|:";
                break;
            case 9: 
                barcode = "|:|::";
                break;
            default:
                break;
        }
        barcode += barcode;
        zipnum = zipnum / 10;
    }
}

public String getBarcode(){
    return barcode;
    }
}

但是,当我的测试者类在用户输入邮政编码后调用此类时,结果不会出现任何结果。请帮忙!为什么没有归还?我不知道自己做错了什么。

5 个答案:

答案 0 :(得分:3)

请注意,您在每个switch-case块中重新分配条形码变量,并在最后再次附加。

您可以使用方法中的临时变量来执行此操作:

public void createBarcode(){
    String tempBarcode = "";
    ...
        case 0:
            tempBarcode = "||:::";
            break;
    ....
    barcode += tempBarcode;

但我认为最干净的解决方案是将其用作没有任何类变量依赖项的实用方法:

public static String calculateBarcode(int zipNumber) {
    StringBuilder barcode = new StringBuilder();
    while (zipNumber > 0) {
        switch (zipNumber % 10) {
            case 0:
                barcode.append("||:::");
                break;
            case 1:
                barcode.append(":::||");
                break;
            ...
            ...
            default:
                break;
        }
        zipNumber = zipNumber / 10;
    }
    return barcode.toString();
}

简单的测试用例:

public static void main(String[] args) {
    System.out.println(ZipcodeUtil.calculateBarcode(95014));
    /* Output: :|::|:::||||::::|:|:|:|:: */
}

答案 1 :(得分:0)

您没有正确地在每次迭代中附加zip值。当您在此步骤中覆盖此值时,该值会丢失

   ...
        case 0: 
                barcode = "||:::";
                break;
   ...

而是使用临时变量来存储当前迭代的邮政编码值

public class Zipcode {

    private String zipcode = "";
    private String barcode = "";
    private int zipnum = 0;

    public Zipcode(int zip_number) {
        zipnum = zip_number;
    }

    public void createBarcode() {
        String temp_barcode = "";
        while (zipnum > 0) {
            System.out.println(zipnum % 10);
            switch (zipnum % 10) {
            case 0:
                temp_barcode = "||:::";
                System.out.println(0);
                break;
            case 1:
                temp_barcode = ":::||";
                System.out.println(1);
                break;
            case 2:
                temp_barcode = "::|:|";
                System.out.println(2);
                break;
            case 3:
                temp_barcode = "::||:";
                System.out.println(3);
                break;
            case 4:
                temp_barcode = ":|::|";
                System.out.println(4);
                break;
            case 5:
                temp_barcode = ":|:|:";
                System.out.println(5);
                break;
            case 6:
                temp_barcode = ":||::";
                System.out.println(6);
                break;
            case 7:
                temp_barcode = "|:::|";
                System.out.println(7);
                break;
            case 8:
                temp_barcode = "|::|:";
                System.out.println(8);
                break;
            case 9:
                temp_barcode = "|:|::";
                System.out.println(9);
                break;
            default:
                break;
            }
            barcode += temp_barcode;
            System.out.println(barcode);
            zipnum = zipnum / 10;
        }
    }

    public String getBarcode() {
        return barcode;
    }

    public static void main(String args[]) {
        Zipcode z = new Zipcode(95014);
        z.createBarcode();
        System.out.println(z.getBarcode()); // output: :|::|:::||||::::|:|:|:|::
    }
}

答案 2 :(得分:0)

以这种方式添加barcode

        case 0: 
            barcode += "||:::";
            break;
        case 1: 
            barcode += ":::||";
            break;
        case 2: 
            barcode += "::|:|";
            break;
        case 3: 
            .....
            .....
            .....

并删除此行

        barcode += barcode;

这将解决您的问题。

答案 3 :(得分:0)

您的条形码变量会在代码中覆盖,因此在createBarcode方法中添加局部变量。试试这个:

public class Zipcode{

private String zipcode = "";
private String barcode = "";
private int zipnum = 0;

public static void main(String[] a){
    Zipcode z = new Zipcode(1568);
    System.out.println(z.zipnum);
    z.createBarcode();
    System.out.println(z.zipnum);
    System.out.println(z.getBarcode());
}

public Zipcode(int zip_number){
    zipnum = zip_number;
}

public void createBarcode(){
    while (zipnum > 0){
        String barcode;
        switch (zipnum % 10)
        {
            case 0:
                barcode = "||:::";
                break;
            case 1:
                barcode = ":::||";
                break;
            case 2:
                barcode = "::|:|";
                break;
            case 3:
                barcode = "::||:";
                break;
            case 4:
                barcode = ":|::|";
                break;
            case 5:
                barcode = ":|:|:";
                break;
            case 6:
                barcode = ":||::";
                break;
            case 7:
                barcode = "|:::|";
                break;
            case 8:
                barcode = "|::|:";
                break;
            case 9:
                barcode = "|:|::";
                break;
            default:
                barcode = "";
                break;
        }
        this.barcode += barcode;
        zipnum = zipnum / 10;
    }
}

public String getBarcode(){
    return barcode;
}

}

答案 4 :(得分:-1)

首先创建一个Zipcode实例,Zipcode代码= Zipcode(9)

然后为该实例调用createBarcode(),所以在我的情况下code.createBarcode()

然后你可以打印出getBarcode()方法。

请记住,getBarcode()仅返回带有条形码的String,但不会将其打印出来。您必须使用打印将其打印出来。

示例:System.out.println(code.getBarcode())