我正在创建一个程序,它是一个简单的银行帐户系统模型。我有一个简单的方法,当被调用时要求用户输入一个名称(String)和一个起始帐户余额(double)并创建一个新的Person对象/ Account对象。
通过使用1-9(int)选项的菜单调用此方法,并说出' 1'按下以创建新帐户。所以代码(在接受字符串之前)有一个默认的input.nextLine();在捕获实际名称输入之前捕获int输入的命令(customerName = input.nextLine();)。
我遇到的问题是我正在尝试向此方法添加异常处理,以确保名称String输入只是字母和空格。当第一次运行而某人输入错误时,它会重新输出"请输入一个名称"但是自从input.nextLine()以来他们必须输入两次名称;还在那里(但没有捕捉菜单输入)。所以我首次添加了一个带有计数器的if / else决策结构,如果(counter == 0)程序运行,那么它保持input.nextLine()并递增计数器,但是如果(counter> 0) )它摆脱了input.nextLine(),以便程序运行正常。
这会导致另一个问题,即如果用户尝试创建多个帐户,它将导致程序在第二次调用时停止打印input.nextLine()并自动假设菜单选项输入的名称是应该是,并发送错误。是否有更好的方法让我按照我的意图工作?
很抱歉,如果描述不是很清楚,那么描述就很难。
以下是代码:
public void menuOptionOne() {
do {
try {
if (counter == 0) { // counter is initially set to 0
System.out.println("Please input the customer's name: ");
input.nextLine(); // catches the menu input
customerName = input.nextLine();
matcher = pattern.matcher(customerName);
counter++; // increments the counter in case the user's input is invalid
if (!matcher.find()) { // if it has non-letter/space characters present, throws exception
throw new Exception("Try again. (Incorrect input: name must contain only letters)");
}
} else if (counter > 0) { // asks the user to input name again, without the input.nextLine() which is intended to catch the menu int input
System.out.println("Please input the customer's name: ");
customerName = input.nextLine();
matcher = pattern.matcher(customerName); // checks the input to ensure it is only letters and/or spaces
if (!matcher.find()) {
throw new Exception("Try again. (Incorrect input: name must contain only letters)");
}
}
continueInput = false;
} catch (Exception ex) {
System.out.println(ex.toString());
}
} while (continueInput);
因此,当连续两次调用它时,它会自动转到第二个决策结构,而没有input.nextLine(),并且捕获菜单输入(1)并抛出异常。如何在每次调用方法时使其正常工作?
这是连续两次调用时的输出(注意:它将菜单输入保存为新客户名称,即使它是一个数字):
Please input the customer's name:
java.lang.Exception: Try again. (Incorrect input: name must contain only letters)
Please enter the new balance:
答案 0 :(得分:1)
您想在输入检索中做两件事:
通过重复使用此方法允许一系列输入。
检查输入的内容,如果不合适则重新开始。
您使用“通过重复使用此方法允许一系列输入”的方式是您的错误的来源
一般来说,当它足够时,你应该倾向于使用最受限制的范围
通过将continueInput
和counter
声明为字段变量而不是局部变量,可以在menuOptionOne()
的调用之间创建耦合。
这解释了你的问题:
这会导致另一个问题,即如果用户尝试创建多个问题 帐户,它会导致程序停止打印 input.nextLine()第二次被调用并自动假设 菜单选项输入是名称应该是什么,并发送一个 错误。是否有更好的方法让我按照我的意图工作?
此代码应该足够了:
public void menuOptionOne() {
// change
int counter = 0;
boolean continueInput = true;
// end change
do {
try {
if (counter == 0) { // counter is initially set to 0
System.out.println("Please input the customer's name: ");
input.nextLine(); // catches the menu input
customerName = input.nextLine();
matcher = pattern.matcher(customerName);
counter++; // increments the counter in case the user's
// input is invalid
if (!matcher.find()) { // if it has non-letter/space
// characters present, throws
// exception
throw new Exception("Try again. (Incorrect input: name must contain only letters)");
}
} else if (counter > 0) { // asks the user to input name again,
// without the input.nextLine()
// which is intended to catch the
// menu int input
System.out.println("Please input the customer's name: ");
customerName = input.nextLine();
matcher = pattern.matcher(customerName); // checks the input
// to ensure it
// is only
// letters
// and/or spaces
if (!matcher.find()) {
throw new Exception("Try again. (Incorrect input: name must contain only letters)");
}
}
continueInput = false;
} catch (Exception ex) {
System.out.println(ex.toString());
}
} while (continueInput);
}
您使用“检查输入内容并在不适合时重新开始”的方式可行,但您可以做得更简单,避免重复。
答案 1 :(得分:0)
啊,我弄明白了我的问题。
我设置变量counter = 0;在我的方法的顶部,然后在底部设置continueInput = true;再次
现在它按预期工作。
所以工作代码如下:
NULL