在打电话课时找不到符号?

时间:2014-04-16 02:23:02

标签: java

我是java的新手,对输入验证有疑问。我试图调用两个类(通过ValidCustomerType和ValidSubtotal的名称)来验证此应用程序的输入。主类的代码如下:

主类:

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;
    }
}

在尝试调用和使用这些类时,我一直收到错误'找不到符号'。我有什么大不了的东西吗?

1 个答案:

答案 0 :(得分:0)

这是你的代码

customerType = vct.getValidCustomerType(sc);

你得到&#34;找不到符号customerType&#34;因为未声明customerType。

声明它,或者像你对其余变量所做的那样,

String customerType = vct.getValidCustomerType(sc);

注意:事实上你的程序之后没有使用它!但这不是当前的问题,与未使用的代码有关。

根据您的评论进行修改

  

但我在哪里以及如何申报呢?如果你向下滚动到我的同时&#39;   代码,我用代码声明它&#39;字符串customerType = sc.next();&#39;

这是在不同的本地范围customerTypeString customerType = sc.next();的范围只是在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"))
......