我正在自学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 + ".");
}
}
我的程序可以编译,但它没有正确跟踪当前的钥匙串(我知道缺少一些东西,但无法弄清楚)。
如果用户选择"1.添加钥匙串",要求他输入要添加的钥匙串数量。此数字将添加到存储在当前钥匙串中的值(从 0 开始)。因此,如果他输入 2,则当前的钥匙串现在持有 2。然后,菜单将重新出现,并询问用户的下一个选择。现在,如果用户选择添加钥匙串或删除钥匙串,则当前钥匙串中的值再次为 0(应为 2)。我不明白如何解决这个问题。有些东西我没有看到或理解。另外,我必须在程序中编写Scanner keyboard = new Scanner(System.in);
四次代码,一次在main中,一次在add_keychains(),一次在remove_keychains(),一次在checkout()中。有什么方法可以只键入一次并允许每个方法都能够使用 scanner 类(不确定我的措辞是否正确)?非常感谢帮助!
int
是Java中的原始类型,这意味着它是按值而不是按引用*传递的。当你在add_keychains
中执行currentKeychains += keychainsAdded
时,它只修改currentKeychains
的本地副本——它对main
变量的副本没有影响。如果希望更改是永久性的,则应执行以下操作之一:
- 添加
currentKeychains
作为类变量,例如private static int currentKeychains
,然后从方法中删除该参数。 - 使用
Integer
而不是int
,因为这将传递对Integer
对象的引用,您可以从中设置值。
惯用的方式是前者;将内部状态保留在类变量中通常是要走的路。