代码在提交给zybooks时无限打印,但在我的IDE中无法打印

时间:2018-03-01 23:53:19

标签: java

编辑:问题是我在ShoppingCartManager和主要部门制作了扫描仪。一旦我从shoppingCartManager调用扫描仪,问题就消失了。

我正在参加计算机科学课程,我们一直在使用Zybooks提交我们的代码。我一直在编写一个购物车程序,其中有一个菜单可以为用户打印选项。我的代码在使用IntelliJ和eclipse时运行正常但是当我将它提交给Zybooks时,它为运行的测试显示的输出是打印提示无限打印..以下代码是主文件中的主要文件。

This is what Zybooks gets for an output (the prompt gets printed until it hits a character limit).

import java.util.ArrayList;
import java.util.Scanner;


public class ShoppingCartManager {
    public static void main(String[] args){
        String customerName, currentDate;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Customer's Name:");
        customerName = sc.nextLine().toString().trim();
        System.out.println("Enter Today's Date:");
        currentDate = sc.nextLine().toString().trim();
        ShoppingCart cart = new ShoppingCart(customerName, currentDate);
        System.out.println();
        System.out.println("Customer Name: "+cart.getCustomerName());
        System.out.println("Today's Date: "+cart.getDate());
        System.out.println();
        printMenu(cart);
}



/**
 * Prints the menu, and builds the shopping cart
 * object passed in as parameter
 */
private static void printMenu(ShoppingCart cart){

        Scanner sc = new Scanner(System.in);
        char choice = ' ';
        System.out.println("MENU");
        System.out.println("a - Add item to cart");
        System.out.println("d - Remove item from cart");
        System.out.println("c - Change item quantity");
        System.out.println("i - Output items' descriptions");
        System.out.println("o - Output shopping cart");
        System.out.println("q - Quit");
        System.out.println();

        while(true){

                System.out.println("Choose an option:");
                if(sc.hasNextLine()) {
                    choice = sc.nextLine().charAt(0);
                }
                if (choice != 'a' && choice != 'd' && choice != 'c' && choice != 'i' && choice != 'o' && choice != 'q'){
                    //An invalid choice is made
                    //Prompt for choice again
                    System.out.println("Choose an option:");
                    if(sc.hasNextLine()) {
                        choice = sc.nextLine().trim().charAt(0);
                    }
                }
                else {
                    switch (choice) {
                        case 'c' : {

                            String name = "";
                            int qty = 0;

                            System.out.println("CHANGE ITEM QUANTITY");
                            System.out.println("Enter the item name:");
                            if(sc.hasNextLine()) {
                                name = sc.nextLine();
                            }
                            System.out.println("Enter the new quantity:");
                            if(sc.hasNextLine()) {
                                qty = sc.nextInt();
                            }
                            ItemToPurchase modItem = new ItemToPurchase();
                            modItem.setName(name);
                            modItem.setQuantity(qty);

                            cart.modifyItem(modItem);

                            choice = ' ';
                            System.out.println("");
                            break;
                        }
                        case 'o': {
                            System.out.println("OUTPUT SHOPPING CART");
                            cart.printTotal();
                            choice = ' ';
                            break;
                        }

                        case 'i': {
                            System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
                            cart.printDescriptions();
                            choice = ' ';
                            break;
                        }

                        case 'a': {
                            String name = "";
                            String desc = "";
                            int price = 0;
                            int qty = 0;
                            System.out.println("ADD ITEM TO CART");
                            System.out.println("Enter item name:");
                            if(sc.hasNextLine()) {
                                name = sc.nextLine();
                            }
                            System.out.println("Enter item description:");
                            if(sc.hasNextLine()) {
                                desc = sc.nextLine();
                            }
                            System.out.println("Enter item price:");
                            if(sc.hasNextLine()) {
                                price = sc.nextInt();
                                sc.nextLine();
                            }

                            System.out.println("Enter item quantity:");
                            if(sc.hasNextLine()) {
                                qty = sc.nextInt();
                                sc.nextLine();
                            }
                            ItemToPurchase itemToPurchase = new ItemToPurchase(name, price, qty, desc);
                            cart.addItem(itemToPurchase);
                            choice = ' ';
                            break;
                        }

                        case 'd': {
                            String itemToRemove = " ";
                            System.out.println("REMOVE ITEM FROM CART");
                            System.out.println("Enter name of item to remove:");
                            if(sc.hasNextLine()) {
                                itemToRemove = sc.nextLine().trim().toString();
                            }
                            cart.removeItem(itemToRemove);
                            choice = ' ';
                            break;

                        }
                        case 'q': {
                            break;
                        }
                    }
            }

    }
}           

}

1 个答案:

答案 0 :(得分:0)

printMenu()方法中的while()循环没有终止条件。我假设您的用户输入'q'时应退出。试试这个

变化:

while(true){

要:

boolean printTheMenu = true;
while(printTheMenu){

然后在'q'的switch语句中执行以下操作

case 'q': {
    printTheMenu = false; //terminate the while loop
    break;
}

您的原始代码只会让您脱离开关参数而不是while循环。现在,当用户输入“q”

时,您的程序将退出