![](/img/trans.png)
[英]Why am I getting a “cannot find symbol” error in my java program when I compile?
[英]Why am I getting a “cannot find symbol” when I compile?
编辑:
感谢大家。
我正在使用NumberFormat类(或方法,我仍然很习惯该术语),但是当我编译程序时,我得到一个“找不到符号”,指向“ NumberFormat.getPercentInstance();”中的小数点 但我不明白为什么。 这是代码:
import java.util.Scanner;
import java.text.NumberFormat;
public class StudentAverages
{
//----------------------------------------------------------------
// Reads a grade from the user and prints averages accordingly.
//----------------------------------------------------------------
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
NumberFormat fmt = new NumberFormat.getPercentInstance();
System.out.print("Enter the name of the first student: ");
String name1 = scan.nextLine();
int lab1, lab2, lab3, min = 0;
System.out.print("Enter the lab assignment grades of "+name1+": ");
lab1 = scan.nextInt();
lab2 = scan.nextInt();
lab3 = scan.nextInt();
System.out.print("Enter the project grades of "+name1+": ");
int proj1 = scan.nextInt();
int proj2 = scan.nextInt();
System.out.print("Enter the midterm grade of "+name1+ ": ");
double mid1 = scan.nextDouble();
System.out.print("Enter the final grade of "+name1+": ");
double fin1 = scan.nextDouble(); // fin used so as to not confuse with CONSTANT final
double avgLab1 = (lab1 + lab2 + lab3) / 3;
double avgProj1 = (proj1 + proj2) / 2;
double grade1 = (25 * avgLab1 + 40 * avgProj1 + 15 * mid1 + 20 * fin1) / 10000;
System.out.println();
System.out.println("Student's name " + name1);
System.out.println("Lab average for "+name1+": " + avgLab1);
System.out.println("Project average for "+name1+": " + avgProj1);
System.out.println("Total grade for "+name1+": " + fmt.format(grade1));
编码新功能。 谢谢你的帮助。 我交叉引用了我正在使用的书中的语法,并进行了检出。 我什至检查了以前的代码,没有做任何不同的事情,或者至少没有看到任何内容。 那么,为什么编译器会说找不到符号?
代替:
NumberFormat fmt = new NumberFormat.getPercentInstance();
它应该是:
NumberFormat fmt = NumberFormat.getPercentInstance();
因为,您想从NumberFormat
类调用静态方法getPercentInstance 。
此外(如Pshemo所说), NumberFormat
类是abstract ,因此您不能使用new
关键字实例化它。
从以下位置删除新的:
NumberFormat fmt = new NumberFormat.getPercentInstance();
您正在调用静态方法。
删除行中的new
关键字
NumberFormat fmt = new NumberFormat.getPercentInstance();
NumberFormat
是abstract
类,这意味着您无法创建它的实例。
您需要扩展它的类的实例,并且可以通过getPercentInstance
方法获取该实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.