【问题标题】:Getting: cannot find symbol error获取:找不到符号错误
【发布时间】:2013-11-18 14:05:15
【问题描述】:

在代码的底部,我尝试将输入传递给一个名为 ISPCharge 的新类。但我不断收到“找不到符号”错误。为什么会这样? (我是初学者。)

我正在尝试传递 packageCode 和 Hours 变量。使用 readHours 和 readPackage 变量是此代码的要求。有什么建议?这个类已经和它应该连接的类在同一个文件夹中。

import java.util.Scanner;

public class ISPBilling {


      static char packageCode;

      static double hours;

      public static void main (String[] Args){


    Scanner keyboard  = new Scanner(System.in);

    System.out.println("Enter the package code (A, B, or C): ");
    packageCode = keyboard.next(".").charAt(0);

     readPackage(packageCode);

  System.out.println("Enter the number of hours used: ");
      hours = keyboard.nextDouble();
      readHours(hours);

      };

      public static double readHours(double hours){

       if(hours >= 0){

        return hours;

      }else 

           System.out.println("Your entry, " + hours + " is invalid.");
           System.out.println();

           return hours = 0;
       };    

    public static char readPackage(char packageCode){

     if (packageCode == 'a' || packageCode == 'A'){

        packageCode = 'A';

        return 'A';

    }else if (packageCode == 'b' || packageCode == 'B'){

        packageCode = 'B';

        return 'B';

      }else if(packageCode == 'c' || packageCode == 'C'){

        packageCode = 'C';

        return 'C';

      }else{  


         System.out.println("Your entry, " + packageCode + " is invalid.");
           System.out.println();


       packageCode = 'A';

       }


       return packageCode;

     };


    public double classcharge(){

    readPackage(packageCode);

    readHours(hours);

    ISPCharge ISPCharger = new ISPCharge(hours, packageCode); // THIS IS IT.

    return hours;

    };

}

/** put your documentation for this class here

查看样式公会的格式 */ 公共类 ISPCharge { // 描述此费用的变量。 私有字符包代码; 私人双小时;

// 在这里声明你的常量。他们应该使用私人 // 可见性修饰符。

  /**************************************************** 
* The constructor sets the package and hours attributes.
* @param pkg The code for the package, A, B, or C
* @param hours The number of hours this month
*/
public double ISPCharge( double hours,char packageCode)
{

  return hours;

 }

/************************************************
* calc charge will decide which package to apply
* and will return the correct cost.
*
* @return The charges for this month.
*/
public double calcCost(){

if (packageCode == 'A' && hours < 10){

    return calcA();

}else if(packageCode == 'A' && hours > 10){

  return calcA() + (hours  - 20) * 2;

}else if(packageCode == 'B'&& hours  < 20){

  return calcB();

}else if(packageCode == 'B' && hours  > 20 ){

  return (hours  - 20) + calcB();

}else if (packageCode == 'C'){

  calcC();

}else 

 return 0;


 return hours;
};


/************************************************
* calcA calculates the charges for package A
*
* The rest is left for you to fill in....
*/
public double calcA(){

 return 9.95;

};
// put the calcA method here

/************************************************
* calcB calculates the charges for package B
*
* The rest is left for you to fill in....
*/
public double calcB(){

return  13.95;


};
// put the calcB method here


/************************************************
* calcC calculates the charges for package C
*
* The rest is left for you to fill in....
*/
public double calcC(){

return 19.95;

};


// put the calcC method here


/** calcTax calculates the tax on the passed charge
 *
 * The rest is left for you to fill in....
 */
 public double calcTax(){

 return calcCost() / 5;


 };

}

【问题讨论】:

    标签: java variables symbols


    【解决方案1】:

    似乎 ISPCharge 类与提到的类不在同一个包中。因此,您需要如下导入类。希望这能解决您的问题。

    import your.package.ISPCharge ;
    

    并使用以下定义更改构造函数,

    因为public double ISPCharge( double hours,char packageCode) 是有效方法但不是构造函数,因为构造函数不能有返回值。

    public  ISPCharge( double hours,char packageCode)
    {
    
      .......
    
     }
    

    【讨论】:

    • 它在我的文件中,只是在这个例子中没有。他们在一起。
    • 这 2 个类是否位于同一个包下。也请分享其他类的代码。
    【解决方案2】:

    ISPCharge 的构造函数不应该有返回类型,因为它的工作是设置对象,而不是返回值。删除单词double 并设置您的私有变量而不是返回值:

    public ISPCharge( double hours,char packageCode)
    {
    
        this.hours=hours;
        this.packageCode=packageCode;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多