Keychain Shop - currentKeychains中的值变量未正确更新 - Java

时间:2016-03-21 18:12:51

标签: java methods

我正在学习Java并且刚刚学习了方法。我试过这个练习Keychains for Sale

 // Exercise 109
    import java.util.Scanner;

    public class KeychainShop {
            public static void main(String[] args) {
                Scanner keyboard = new Scanner(System.in);

                int selection, currentKeychains = 0, price = 10;

                System.out.println("Welcome to the Keychain Shop!\n");

                do {
                    System.out.println("Select 1, 2, 3, or 4");
                    System.out.println("1. Add Keychains");
                    System.out.println("2. Remove Keychains");
                    System.out.println("3. View Order");
                    System.out.println("4. Checkout");

                    System.out.print("\nWhat would you like to do? ");
                    selection = keyboard.nextInt();
                    System.out.println();

                    if (selection == 1) {
                        System.out.println("You now have " + add_keychains(currentKeychains) + " keychains.");
                        System.out.println();
                    } 
                    else if (selection == 2) {
                        System.out.println("You now have " + remove_keychains(currentKeychains) + " keychains.");
                        System.out.println();
                    } 
                    else if (selection == 3) {
                        view_order(currentKeychains, price);
                        System.out.println();
                    } 
                    else if (selection == 4) {
                        checkout(currentKeychains, price);
                    }
                } while (selection != 4);
            }

            public static int add_keychains(int currentKeychains) {
                Scanner keyboard = new Scanner(System.in);
                System.out.print("You have " + currentKeychains + " keychains. How many would you like to add? ");
                int keychainsAdded = keyboard.nextInt();

                currentKeychains += keychainsAdded;

                return currentKeychains;
            }

            public static int remove_keychains(int currentKeychains) {
                Scanner keyboard = new Scanner(System.in);
                System.out.print("You have " + currentKeychains + " keychains. How many would you like to remove? ");
                int keychainsRemoved = keyboard.nextInt();

                currentKeychains -= keychainsRemoved;

                return currentKeychains;
            }

            public static void view_order(int currentKeychains, int price) {
                System.out.println("You are currently buying " + currentKeychains + " keychains.");
                System.out.println("Each keychain costs $" + price + ".");

                int totalCost = currentKeychains * price;
                System.out.println("Your current total is $" + totalCost);
            }

            public static void checkout(int currentKeychains, int price) {
                Scanner keyboard = new Scanner(System.in);
                System.out.print("Please enter your name: ");
                String name = keyboard.nextLine();
                System.out.println("You have bought " + currentKeychains + " keychains.");

                int totalCost = currentKeychains * price;
                System.out.println("Your total is $" + totalCost);
                System.out.println("Thanks for shopping with us today, " + name + ".");
            }
    }

我的程序编译但是没有正确跟踪当前的Keychains(我知道有些东西缺失了,但无法弄明白)。

如果用户从菜单中选择“1.添加钥匙串”,则会要求他输入他想要添加的钥匙串数量。此数字将添加到currentKeychains(从0开始)中存储的值。因此,如果他输入2,则currentKeychains现在保持2.然后,菜单重新出现并询问用户的下一个选择。现在,如果用户选择添加钥匙串或删除钥匙串,则currentKeychains中的值再次为0(应为2)。我不明白如何解决这个问题。有一些我没有看到或理解的东西。此外,我必须在程序中编码Scanner keyboard = new Scanner(System.in);四次,一次在main中,一次在add_keychains()中,一次在remove_keychains()中,一次在checkout()中。有没有什么办法我可以只输入一次,并允许每种方法都能使用扫描仪类(不确定我说的是否正确)?非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

int是Java中的原始类型,这意味着它是通过值而不是通过引用*传递的。当您在currentKeychains += keychainsAdded中执行add_keychains时,它只会修改currentKeychains本地副本 - 它对main的{​​{1}}副本没有影响那个变量。如果您希望更改是持久的,则应执行以下操作之一:

  • currentKeychains添加为类变量,例如private static int currentKeychains,并从方法中删除该参数。
  • 使用Integer代替int,因为这会传递对Integer对象的引用,您可以从中设置值。

惯用的方式是前者;保持内部状态在类变量中通常是要走的路。