我尝试验证只应接受customerType
R
或C
的输入。它不区分大小写。我的代码出错了,它说我的String customerType
丢失了:
import java.text.NumberFormat;
import java.util.InputMismatchException;
import java.util.Scanner;
public class InvoiceApp
{
public static void main(String[] args)
{
// Begin input
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
// create catch block for customerType(r or c)
try
{
System.out.print("Enter customer type (r/c): ");
String customerType = sc.next();
}
catch (InputMismatchException e)
{
if (customerType != "r" || "c")
{
sc.next();
System.out.println("Enter a valid customer type r or c");
continue;
}
else
{
System.out.print("Enter subtotal: ");
}
}
double subtotal = sc.nextDouble();
// get the discount percent
double discountPercent = 0;
if (customerType.equalsIgnoreCase("R"))
{
if (subtotal < 100)
discountPercent = 0;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .1;
else if (subtotal >= 250)
discountPercent = .2;
}
else if (customerType.equalsIgnoreCase("C"))
{
if (subtotal < 250)
discountPercent = .2;
else
discountPercent = .3;
}
else
{
discountPercent = .1;
}
// calculate the discount amount and total
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// format and display the results
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println("Discount percent: " + percent.format(discountPercent)
+ "\n" + "Discount amount: " + currency.format(discountAmount) + "\n"
+ "Total: " + currency.format(total) + "\n");
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}
答案 0 :(得分:1)
首先,您的错误原因是customerType字符串的范围仅限于声明它的 try 块。由于无法在尝试块之外访问(在您的情况下,在 catch 块内),编译器会生成错误。
并且您不需要将代码放在try / catch块中,因为没有可以生成异常的代码。
要解决这个问题,可以采用多种方式:
在try try 之外声明customerType字符串,并将其初始化为null或空字符串。
第二种方式(我更喜欢尝试使用try / catch块)是将所有逻辑移到try块中。这样您就不必在try块之外声明它,并且所有使用customerType的代码都将保持干净(没有任何错误),因此您不必担心错误。并且,还包括在catch块内发生上述异常时执行的代码。您的catch块应该只包含仅在发生异常时运行的代码。
这些建议与您的代码问题有关。
您的代码的另一个问题是您在 if 语句中输入了错误的条件。正确的方法是:
if (customerType.equals("r") || customerType.equals("c")){
...
}
除此之外,您需要声明discountPercent(您没有)并在使用discountPercent之前初始化为0。
无论如何,这里是修正代码的主要方法(其余部分与你的相同)。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n")){
System.out.print("Enter customer type (r/c): ");
String customerType = sc.next();
if (customerType.equalsIgnoreCase("r")||customerType.equalsIgnoreCase("c")){
sc.next();
System.out.println("Enter a valid customer type r or c");
continue;
}
else{
System.out.print("Enter subtotal: ");
}
double subtotal = sc.nextDouble();
double discountPercent = 0;
if (customerType.equalsIgnoreCase("R")){
if (subtotal < 100)
discountPercent = 0;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .1;
else if (subtotal >= 250)
discountPercent = .2;
}
else if (customerType.equalsIgnoreCase("C")){
if (subtotal < 250)
discountPercent = .2;
else
discountPercent = .3;
}
else{
discountPercent = .1;
}
// calculate the discount amount and total
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// format and display the results
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println(
"Discount percent: " + percent.format(discountPercent)
+ "\n" +
"Discount amount: " + currency.format(discountAmount)
+ "\n" +
"Total: " + currency.format(total) + "\n");
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
如果您有任何疑问,请告诉我。我将非常乐意为您提供帮助。
修改1:
根据@Andreas的建议,我已经纠正了我输错的条件陈述。
答案 1 :(得分:0)
这样可行,但是在没有更新总销售价值的情况下循环客户继续提示是没有意义的。
import java.text.NumberFormat;
import java.util.Scanner;
public class InvoiceApp
{
public static void main(String[] args)
{
// Begin input
Scanner sc = new Scanner(System.in);
String customerType = null;
boolean choice = true;
while (choice)
{
// get the input from the user
System.out.print("Enter customer type (r/c): ");
customerType = sc.next();
while (!customerType.equalsIgnoreCase("r")
&& !customerType.equalsIgnoreCase("c"))
{
System.out.println("Enter a valid customer type r or c");
customerType = sc.next();
}
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();
// get the discount percent
double discountPercent = 0;
if (customerType.equalsIgnoreCase("R"))
{
if (subtotal < 100)
discountPercent = 0;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .1;
else if (subtotal >= 250)
discountPercent = .2;
}
else if (customerType.equalsIgnoreCase("C"))
{
if (subtotal < 250)
discountPercent = .2;
else
discountPercent = .3;
}
else
{
discountPercent = .1;
}
// calculate the discount amount and total
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// format and display the results
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println("Discount percent: " + percent.format(discountPercent)
+ "\n" + "Discount amount: " + currency.format(discountAmount) + "\n"
+ "Total: " + currency.format(total) + "\n");
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
String userInput = sc.next();
choice = userInput.equalsIgnoreCase("y");
System.out.println();
}
sc.close();
}
}