for循环并不像它应该的那样工作

时间:2014-10-16 07:10:29

标签: java

程序会询问用户项目的代码,然后程序会将项目的状态更改为不可用。代码工作正常,它会更改状态,但其他内部的代码仍在运行并且找不到打印项目

这是代码

public void stopSellingItem(){
    boolean invalidInput;
    int q = -1;
    String u = "Unvailable";
    do {        
        try {        
            invalidInput = false;
    System.out.println("Enter the item's code you want to stop selling it : ");
        q = s.nextInt();
        out: for (int i = 0; i<items.length; i++){
            if(q == items[i].getCode()){
                items[i] = new Items(items[i].getCode(), items[i].getDescription(), items[i].getQuantity(),
                        items[i].getcostPrice(), items[i].getsellingPrice(),
                        u, items[i].getDiscount());
                break out;
            }else if(q != items[i].getCode()){
                System.out.println("The Item is not found");
            }
        }

        } catch (InputMismatchException e) {
    System.out.println("Please enter a valid code [Numbers Only]");
            s.next();
    invalidInput = true;  // This is what will get the program to loop back
        }
} while (invalidInput);
}

我做了你们所有的建议,但没有任何效果 这是我的最终代码仍然存在同样的问题

public void stopSellingItem(){
    boolean invalidInput;
    int q = -1;
    String u = "Unvailable";
    boolean y = true;
    do {        
        try {        
            invalidInput = false;
    System.out.println("Enter the item's code you want to stop selling it : ");
        q = s.nextInt();
        for (int i = 0; i<items.length; i++){

            while(items[i].getCode()==q){
                items[i].setStatus(u);
                y = false;
                break;
            }
            if(y)
                System.out.println("The item is not found");
        }

        } catch (InputMismatchException e) {
    System.out.println("Please enter a valid code [Numbers Only]");
            s.next();
    invalidInput = true;  // This is what will get the program to loop back
        }
} while (invalidInput);
}

2 个答案:

答案 0 :(得分:2)

break正在运行,但由于第一项而非最后一项,您正在打印The Item is not found。添加一个布尔值来捕捉大小写。然后你也可以省略休息。

    // add a boolean
    boolean itemFound = false;
    for (int i = 0; i<items.length; i++){
        if(q == items[i].getCode()){
            items[i] = new Items(items[i].getCode(), items[i].getDescription(), items[i].getQuantity(),
                    items[i].getcostPrice(), items[i].getsellingPrice(),
                    u, items[i].getDiscount());
            itemFound = true;
        }
    }

    if (!itemFound){
        System.out.println("The Item is not found");
    }

答案 1 :(得分:0)

终于在&#34;荒谬的心灵&#34;

的帮助下解决了这个问题

这是解决后的代码

public void stopSellingItem(){
    boolean invalidInput;
    int q = -1;
    String u = "Unvailable";
    boolean y = false;
    do {        
        try {        
            invalidInput = false;
    System.out.println("Enter the item's code you want to stop selling it : ");
        q = s.nextInt();
        out: for (int i = 0; i<items.length; i++){

            if(items[i].getCode()==q){
                items[i].setStatus(u);
                y = false;
                break out;
            }
            if(!y)
                System.out.println("The item is not found");
            break out;
        }

        } catch (InputMismatchException e) {
    System.out.println("Please enter a valid code [Numbers Only]");
            s.next();
    invalidInput = true;  // This is what will get the program to loop back
        }
} while (invalidInput);
}

我所做的一切都被推出:在外循环中,当我打破我从外循环中断时

感谢所有