我想制作一个代码,只有客户“R”,“C”和“T”可以制作小计。每当我输入另一个字母(即“S”,“L”,“A”等)时,我希望输出无效。但是,当我输入的字母不是R,C或T时,我仍然要求输入小计。一如既往的帮助。谢谢。代码如下。
======================
import java.text.NumberFormat;
import java.util.Scanner;
public class ValidatedInvoiceApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
System.out.print("Enter customer type (r/c/t): ");
String customerType = sc.next();
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();
// call the DiscountPercent method
double discountPercent = getDiscountPercent(
customerType,subtotal);
// 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();
}
}
private static double getDiscountPercent(String customerType,double subtotal)
{
double discountPercent = 0;
if (customerType.equalsIgnoreCase("R"))
{
if (subtotal < 100)
discountPercent = 0;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .1;
else if (subtotal >= 250 && subtotal < 500)
discountPercent = .25;
else
discountPercent = .30;
}
else if (customerType.equalsIgnoreCase("C"))
{
discountPercent = .20;
}
else if (customerType.equalsIgnoreCase("T"))
{
if (subtotal < 500)
discountPercent = .40;
else if (subtotal >= 500)
discountPercent = .50;
}
else
{
System.out.println("Error! Invalid Customer Type. Please Try Again. \n");
sc.nextLine();
}
return discountPercent;
}
private static class sc {
private static void nextLine() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public sc() {
}
}
}
答案 0 :(得分:1)
您不会对输入进行任何类型的检查。
System.out.print("Enter customer type (r/c/t): ");
String customerType = sc.next();
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();
你应该使用do-while(如果你想要另一种行为,可以使用其他的东西)
String customerType;
do
{
System.out.print("Enter customer type (r/c/t): ");
customerType = sc.next();
}
while (!customerType.equalsIgnoreCase("r") && !customerType.equalsIgnoreCase("c") && !customerType.equalsIgnoreCase("t"));
使用此代码,在他写r,c或t之前,他将被要求再次插入。
无论如何,你应该考虑使用char而不是String。
答案 1 :(得分:0)
您要求他们插入后,您需要检查客户类型。你的主要方法应该是这样的 -
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n")) {
// get the input from the user
System.out.print("Enter customer type (r/c/t): ");
String customerType = sc.next();
double subtotal = 0;
if (customerType.equalsIgnoreCase("r") || customerType.equalsIgnoreCase("c") || customerType
.equalsIgnoreCase("t")) {
System.out.print("Enter subtotal: ");
subtotal = sc.nextDouble();
// call the DiscountPercent method
double discountPercent = getDiscountPercent(
customerType, subtotal);
// 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");
} else {
System.out.println("Error! Invalid Customer Type. Please Try Again. \n");
sc.nextLine();
}
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}