【问题标题】:Cannot find symbol error for object when it has been initialised and defined初始化和定义对象时找不到符号错误
【发布时间】:2014-04-07 05:21:51
【问题描述】:
 1 // This program reads in an item's cost and some coupons' information,
 2 // and then determines which is the best coupon to use and the amount 
 3 // to pay.
 4 
 5 import java.util.*;
 6 
 7 
 8 public class Redeem {
 9         
 10     public static void main(String[] args) {
 11         Scanner sc = new Scanner(System.in);
 12         double price = sc.nextDouble();
 13         int size = sc.nextInt();
 14         int i=0;
 15         double amtPay = price;
 16         double negativeAmt= -99999.99;
 17         
 18         ArrayList<Coupon>coupons = new ArrayList<Coupon>(size);
 19         
 20         for(i=0; i<size; i++)   {
 21             Coupon newCoupon = new Coupon(sc.next(), sc.nextDouble());
 22             coupons.add(i, newCoupon);
 23             sc.nextLine();
 24         }   
 25             
 26         for(i=0; i<size; i++)   {
 27             double temp = (coupons.get(i)).payment(price);
 28             if (temp < 0)   {   
 29                 if (temp > negativeAmt) {
 30                     negativeAmt = temp;
 31                     Coupon bestCoupon = new Coupon(coupons.get(i));
 32                 }   
 33                 amtPay = 0.00;
 34             }   
 35             else    {
 36                 if (amtPay != 0)    {
 37                     if (temp < amtPay)  {
 38                         amtPay = temp;
 39                     }
 40                     Coupon bestCoupon = new Coupon(coupons.get(i));
 41                 }   
 42             }       
 43         }       
 44         System.out.println("Best choice: " + bestCoupon);
 45         System.out.printf("You need to pay $%.2f\n", amtPay);
 46     }
 47 }

我不断收到的错误是

Redeem.java:44: error: cannot find symbol
                System.out.println("Best choice: " + bestCoupon);
                                                     ^
  symbol:   variable bestCoupon
  location: class Redeem
1 error

为什么它看不到它是 Coupon 类的对象?我在此之前编译了我的 Coupon 类(使用 toString 方法,因此 println 应该检测到一个字符串)并且仍然有这个问题:( helppp

【问题讨论】:

  • 优惠券bestCoupon可以在它声明的块内访问。

标签: java object compiler-errors symbols


【解决方案1】:

因为您在if 的范围内声明了它,所以它只在那里为人所知:

if(temp > negativeAmt) {
    negativeAmt = temp;
    Coupon bestCoupon = new Coupon(coupons.get(i));  
    //bestCoupon is known here
} 
//But not here

更一般的例子:

if(something) {
   int a;
   if(somethingElse) {
       int b;
       a = b; //OK
   }
   b = a; //Error, b is not known here
}

【讨论】:

    【解决方案2】:

    范围很重要。

    您将该变量的范围限制在 if 之外。如果该变量不可用,则除此之外。

    将您的声明 Coupon bestCoupon 移动到 for 循环的顶部。

    然后在循环边

     bestCoupon = new Coupon(coupons.get(i));
    

    例子:

    Coupon bestCoupon= null;
        for{
          if(){
            bestCoupon=...
           }
        }
    //Access here.
    

    【讨论】:

      【解决方案3】:
       Coupon bestCoupon =null;
       for(i=0; i<size; i++)   {
          ...
          bestCoupon = new Coupon(coupons.get(i)); //not Coupon bestCoupon = new Coupon(coupons.get(i));
          ...
       }
       System.out.println("Best choice: " + bestCoupon);
      

      【讨论】:

        【解决方案4】:

        更改Coupon bestCoupon like的范围--

            public class Redeem {
        
                public static void main(String[] args) {
                    Scanner sc = new Scanner(System.in);
                    double price = sc.nextDouble();
                    int size = sc.nextInt();
                    int i=0;
                    double amtPay = price;
                    double negativeAmt= -99999.99;
                    Coupon bestCoupon  =null; //<-- Declaration of Coupon
                    ArrayList<Coupon>coupons = new ArrayList<Coupon>(size);
        
                    for(i=0; i<size; i++)   {
                        Coupon newCoupon = new Coupon(sc.next(), sc.nextDouble());
                        coupons.add(i, newCoupon);
                        sc.nextLine();
                    }   
        
                    for(i=0; i<size; i++)   {
                        double temp = (coupons.get(i)).payment(price);
                        if (temp < 0)   {   
                            if (temp > negativeAmt) {
                                negativeAmt = temp;
                                bestCoupon = new Coupon(coupons.get(i));
                            }   
                            amtPay = 0.00;
                        }   
                        else    {
                            if (amtPay != 0)    {
                                if (temp < amtPay)  {
                                    amtPay = temp;
                                }
                                bestCoupon = new Coupon(coupons.get(i));
                            }   
                        }       
                    }       
                    System.out.println("Best choice: " + bestCoupon);
                    System.out.printf("You need to pay $%.2f\n", amtPay);
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-22
          • 1970-01-01
          • 2012-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-12
          • 2013-04-18
          相关资源
          最近更新 更多