Java 调用类时找不到符号?
我是java新手,对输入验证有疑问。我正在尝试调用两个类(名称分别为ValidCustomerType和ValidSubtotal),以验证此应用程序的输入。主要类别的代码如下所示: 主要类别:Java 调用类时找不到符号?,java,Java,我是java新手,对输入验证有疑问。我正在尝试调用两个类(名称分别为ValidCustomerType和ValidSubtotal),以验证此应用程序的输入。主要类别的代码如下所示: 主要类别: import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Scanner; public class InvoiceApp { static Scanner sc = new Scanner(
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class InvoiceApp
{
static Scanner sc = new Scanner(System.in);
static NumberFormat currency = NumberFormat.getCurrencyInstance();
static NumberFormat percent = NumberFormat.getPercentInstance();
static CostelloHeading ch = new CostelloHeading();
static CostelloDate cd = new CostelloDate();
static ValidCustomerType vct = new ValidCustomerType();
static ValidSubtotal vst = new ValidSubtotal();
public static void main(String[] args)
{
DecimalFormat cf = new DecimalFormat("$#,##0.00");
DecimalFormat nf = new DecimalFormat("#,##0.00");
///NumberFormat cf = NumberFormat.getCurrencyInstance();
// NumberFormat nf = NumberFormat.getNumberInstance();
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMaximumFractionDigits(1);
pf.setMinimumFractionDigits(1);
//nf.setMaximumFractionDigits(2);
//nf.setMinimumFractionDigits(2);
customerType = vct.getValidCustomerType(sc);
subtotal = vst.getValidSubtotal(sc);
String choice = "y";
ch.getHeading("Assignment 4 - Validate Input Data");
System.out.printf(" Invoice Application \n\n");
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
System.out.print(" Enter customer type (C, J, R or T) or an X to Exit -> ");
String customerType = sc.next();
if (customerType.equalsIgnoreCase("X"))
{
System.out.printf("\n Program terminated by the User.\n");
CostelloDate.printfDate();
CostelloDate.printfTime();
System.exit(0);
}
System.out.print(" Enter subtotal -> ");
double subtotal = sc.nextDouble();
// get the discount percent
double discountPercent = getDiscountPercent(customerType, subtotal);
// calculate the discount amount and total
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// format and display the results
System.out.printf("\n Customer Type Selected: %s\n", customerType.toUpperCase());
System.out.printf("%12s %10s %12s %10s\n", "Invoice", "Discount", "Discount", "Billed");
System.out.printf("%12s %10s %12s %10s\n", "Amount", "Percent", "Amount", "Amount");
System.out.printf("%12s %10s %12s %10s\n", "--------", "--------", "--------", "--------");
System.out.printf(" $%,9.2f %8.1f%% $ %,8.2f $ %8.2f\n\n", subtotal,(discountPercent * 100), discountAmount, total);
}
}
private static double getDiscountPercent(String customerType, double subtotal)
{
double discountPercent = 0;
if (customerType.equalsIgnoreCase("C"))
{
discountPercent = .188;
}
else if (customerType.equalsIgnoreCase("J"))
{
discountPercent = .23;
}
else if (customerType.equalsIgnoreCase("R"))
{
if (subtotal < 100)
discountPercent = .055;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .121;
else if (subtotal >= 250 && subtotal < 500)
discountPercent = .222;
else discountPercent = .333;
}
else if (customerType.equalsIgnoreCase("T"))
{
if (subtotal < 500)
discountPercent = .404;
else discountPercent = .525;
}
else
{
discountPercent = .1;
}
return discountPercent;
}
}
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
导入java.util.Scanner;
公共类InvoiceApp
{
静态扫描仪sc=新扫描仪(System.in);
静态NumberFormat currency=NumberFormat.getCurrencyInstance();
静态NumberFormat百分比=NumberFormat.getPercentInstance();
静态CostelloHeading ch=新CostelloHeading();
静态CostelloDate cd=新CostelloDate();
静态ValidCustomerType vct=新的ValidCustomerType();
静态ValidSubtotal vst=新的ValidSubtotal();
公共静态void main(字符串[]args)
{
DecimalFormat cf=新的DecimalFormat($#,##0.00”);
DecimalFormat nf=新的DecimalFormat(“0.00”);
///NumberFormat cf=NumberFormat.getCurrencyInstance();
//NumberFormat nf=NumberFormat.getNumberInstance();
NumberFormat pf=NumberFormat.getPercentInstance();
pf.setMaximumFractionDigits(1);
pf.setMinimumFractionDigits(1);
//nf.setMaximumFractionDigits(2);
//nf.setMinimumFractionDigits(2);
customerType=vct.getValidCustomerType(sc);
小计=vst.getValidSubtotal(sc);
字符串选择=“y”;
ch.getHeading(“作业4-验证输入数据”);
System.out.printf(“发票应用程序\n\n”);
而(!choice.equalsIgnoreCase(“n”))
{
//从用户那里获取输入
系统输出打印(“输入客户类型(C、J、R或T)或X以退出->”;
字符串customerType=sc.next();
if(customerType.equalsIgnoreCase(“X”))
{
System.out.printf(“\n程序被用户终止。\n”);
CostelloDate.printfDate();
CostelloDate.printfTime();
系统出口(0);
}
系统输出打印(“输入小计->”;
双倍小计=sc.nextDouble();
//打九折
双重折扣百分比=getDiscountPercent(客户类型,小计);
//计算折扣金额和总额
双重折扣金额=小计*折扣百分比;
双倍合计=小计-折扣金额;
//格式化并显示结果
System.out.printf(“\n所选客户类型:%s\n”,customerType.toUpperCase());
System.out.printf(“%12s%10s%12s%10s\n”、“发票”、“折扣”、“折扣”、“账单”);
System.out.printf(“%12s%10s%12s%10s\n”、“金额”、“百分比”、“金额”、“金额”);
System.out.printf(“%12s%10s%12s%10s\n”、“-----------”、“-----------”、“-----------”、“-----------”;
System.out.printf($%,9.2f%8.1f%%%,8.2f$%8.2f\n\n),小计(折扣百分比*100),折扣金额,总计);
}
}
私有静态双getDiscountPercent(字符串customerType,双小计)
{
双倍折扣百分比=0;
if(customerType.equalsIgnoreCase(“C”))
{
折扣百分比=.188;
}
else if(customerType.equalsIgnoreCase(“J”))
{
折扣百分比=.23;
}
else if(customerType.equalsIgnoreCase(“R”))
{
如果(小计<100)
折扣百分比=.055;
否则如果(小计>=100&&小计<250)
折扣百分比=.121;
否则如果(小计>=250&&小计<500)
折扣百分比=.222;
其他折扣百分比=.333;
}
else if(customerType.equalsIgnoreCase(“T”))
{
如果(小计<500)
折扣百分比=.404;
其他折扣百分比=.525;
}
其他的
{
折扣百分比=.1;
}
退货折扣百分比;
}
}
在尝试调用和使用这些类时,我不断收到一个错误“找不到符号”。有什么我遗漏的吗?这是你的代码
customerType=vct.getValidCustomerType(sc)代码>
由于未声明customerType,因此会出现“找不到符号customerType”
像这样声明,或者像你对其他变量所做的那样
String customerType = vct.getValidCustomerType(sc);
注意:事实上,此后您的程序不会使用它!。但这不是当前的问题,这与未使用的代码有关
根据您的评论进行编辑
但是我在哪里以及如何申报呢?如果你向下滚动到我的“while”
代码,我用代码'String customerType=sc.next();'
这是在不同的本地范围内。String customerType=sc.next()中的customerType
的范围
仅在while循环中是一个局部作用域
基本上你的代码应该是这样的
....
String customerType;
customerType = vct.getValidCustomerType(sc);
...
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
System.out.print(" Enter customer type (C, J, R or T) or an X to Exit -> ");
customerType = sc.next();
if (customerType.equalsIgnoreCase("X"))
......
什么符号。向我们展示错误堆栈跟踪,其中类静态CostelloHeading ch=new CostelloHeading();静态CostelloDate cd=新CostelloDate();静态ValidCustomerType vct=新的ValidCustomerType();静态ValidSubtotal vst=新的ValidSubtotal()代码>-它们在另一个jar或另一个包中吗?您没有导入它们吗?我在“默认包”中有这些文件:CostelloDate.java CostelloHeading.java ValidCustomerType.java和ValidSubtotal.java默认包中提到的类文件都正确编码Hanks prash,我在代码中更改了这一点,我得到的错误是:“不兼容的类型:字符串无法转换为ValidCustomerType”重试,如上所述