2016-03-21 50 views
0

我正在自学Java并刚学过方法。我曾尝试这个练习Keychains for SaleKeychain Shop - currentKeychains变量中的值未正确更新 - Java

// 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 + "."); 
      } 
    } 

我的程序编译,但它无法正确跟踪currentKeychains(我知道有一些人失踪,但无法弄清楚)的。

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

回答

1

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

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

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

+0

有没有办法通过保持参数来保持更改持久?什么是私人? – camelCoder

+0

不是那些参数,没有。我会用几个备选方案更新我的答案。 'private'是一个*访问修饰符*,表示其他类不能访问该变量 - 您很可能会很快了解访问修饰符。 –