Keychain Shop-currentKeychains变量中的值未正确更新-Java
我正在自学Java,刚刚学会了一些方法。我尝试过这个练习 我的程序可以编译,但不能正确地跟踪当前的密钥链(我知道缺少一些东西,但无法找到它)Keychain Shop-currentKeychains变量中的值未正确更新-Java,java,methods,Java,Methods,我正在自学Java,刚刚学会了一些方法。我尝试过这个练习 我的程序可以编译,但不能正确地跟踪当前的密钥链(我知道缺少一些东西,但无法找到它) 如果用户从菜单中选择“1.添加钥匙链”,系统会要求用户输入要添加的钥匙链数量。此数字将添加到currentKeychains中存储的值(从0开始)。因此,如果他输入2,currentKeychains现在可以保存2。然后,菜单再次出现,并要求用户进行下一次选择。现在,如果用户选择添加钥匙链或删除钥匙链,currentKeychains中的值再次为0(应为2
如果用户从菜单中选择“1.添加钥匙链”,系统会要求用户输入要添加的钥匙链数量。此数字将添加到currentKeychains中存储的值(从0开始)。因此,如果他输入2,currentKeychains现在可以保存2。然后,菜单再次出现,并要求用户进行下一次选择。现在,如果用户选择添加钥匙链或删除钥匙链,currentKeychains中的值再次为0(应为2)。我不知道如何解决这个问题。有些东西我看不见也不明白。另外,我必须编码
扫描仪键盘=新扫描仪(System.in)在程序中编码>四次,一次在main中,一次在add_keychains(),一次在remove_keychains()中,一次在checkout()中。是否有任何方法可以只键入一次,并允许每个方法都能够使用scanner类(不确定我的措辞是否正确)?非常感谢您的帮助 int
是Java中的一种基本类型,这意味着它是通过值传递的,而不是通过引用传递的*。当您在add_keychains
中执行currentKeychains+=keychainsAdded
时,它只修改currentKeychains的本地副本
——它对该变量的main
副本没有影响。如果希望更改保持不变,应执行以下操作之一:
- 添加
currentKeychains
作为类变量,例如private static int currentKeychains
,并从方法中删除该参数
- 使用
整数
而不是整数
,因为这将传递对整数
对象的引用,您可以从中设置值
惯用的方式是前者;在类变量中保持内部状态通常是一种方法。有没有办法通过保持参数来保持更改的持久性?什么是私有的?不是那些参数,不。我会用一些替代方案更新我的答案private
是一个访问修饰符,表示其他类无法访问该变量——您可能很快就会了解访问修饰符。
// 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 + ".");
}
}