我试图编译一个 Java 程序,但它生成了一个错误。
有人可以告诉我哪里出了错误以及如何修复它吗?
import java.io.*;
import java.util.*;
class detail
{
String name;
int age;
float salary;
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter name: ");
name=new sc.next();
System.out.println("enter age: ");
age=new sc.nextInt();
System.out.println("enter salary: ");
salary=new sc.nextFloat();
}
void display()
{
System.out.println("name: "+name);
System.out.println("age: "+age);
System.out.println("salary: "+salary);
}
}
class person
{
public static void main(String arr[])
{
detail p=new detail();
p.getdata();
p.display();
}
}
最佳答案
您有 3 行包含
new sc
删除 new
因为 sc
不是一个类,而是一个变量,并且您已经在此行中创建了 Scanner
对象:
Scanner sc=new Scanner(System.in);
只有在构造函数调用后面或在方法引用中的 java 8 中才允许使用 new
关键字,但如果您键入 new sc
,则情况并非如此。
关于java - 扫描仪类包不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32156425/