Java 尝试使用扫描仪时“无法解析为类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25819174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Cannot be resolved to a type" when attempting to use Scanner
提问by user4036695
when i run following code it shows error that scanner cannot be resolved to type. i checked that jre is installed and version is 1.7 what else do i need to check ? please help.
当我运行以下代码时,它显示无法解析扫描仪输入的错误。我检查了 jre 已安装并且版本是 1.7 我还需要检查什么?请帮忙。
public class student {
String name;
int rollno;
public void get(String nm, int rno)
{ name=nm;
rollno=rno;
}
public void display()
{ System.out.println("Name of student is :" +name);
System.out.println("Roll no of student is :" +rollno);
}
public static void main(String args[])
{
int i ;
int r1;
String n1;
student obj[]= new student[10];
Scanner sc=new Scanner(System.in);
for(i=0;i<10;i++)
{
obj[i]= new student();
}
for(i=0;i<10; i++)
{ System.out.println("Enter name:");
n1=sc.next();
sc.nextLine();
System.out.println("Enter roll no :");
r1=sc.nextInt();
obj[i].get(n1,r1) ;
obj[i].display() ;
}
}
}
回答by nanofarad
You need to also import the class itself. At the very top of the file, above public class student
, you need to add:
您还需要导入类本身。在文件的最顶部,上面public class student
,您需要添加:
import java.util.Scanner;
In addition, I'd like to pose a few more possible corrections:
此外,我想提出更多可能的更正:
- Class names should be
PascalCase
- Your code should have consistent indentation. Ctrl+Shift+F is your friend here.
- 类名应该是
PascalCase
- 您的代码应该具有一致的缩进。Ctrl+Shift+F 是你的朋友。
回答by Jean-Fran?ois Savard
I tried the code myself and it works. Therefore, it is a configuration problem. Since you tried to import java.util.Scanner, as hexafraction suggested, then I suppose the JRE is not properly configured.
我自己尝试了代码并且它有效。因此,这是一个配置问题。由于您尝试导入 java.util.Scanner,正如 hexafraction 所建议的那样,那么我想 JRE 没有正确配置。
Try :
尝试 :
- Right clicking your project name -> Click properties -> Click Java Build Path
- Select the libraries tab
- Click on add class folder (at the right) then select your Class.
- 右键单击您的项目名称 -> 单击属性 -> 单击 Java 构建路径
- 选择库选项卡
- 单击添加班级文件夹(在右侧),然后选择您的班级。
Edit : Even tho it would not really solve the problem, copy the src folder in a new project would probably solve your issue.
编辑:即使它不能真正解决问题,在新项目中复制 src 文件夹也可能会解决您的问题。
回答by DeepInJava
Check your code's compilation level by right click your project in eclipse and click properties.
通过在 Eclipse 中右键单击您的项目并单击属性来检查您的代码的编译级别。
It might point to 1.6 or lower. If it is the case point it to 1.7
它可能指向 1.6 或更低。如果是这种情况,请将其指向 1.7
This might solve your problem.
这可能会解决您的问题。
I hope it helps.
我希望它有帮助。
回答by Monica Antony
Just use import java.util.Scanner;
or use import java.util.*;
只是使用import java.util.Scanner;
或使用import java.util.*;
回答by Vijay Bhangur
Right click on your package > Navigate to properties > Click on java build path > Click on libraries tab > Click on add library > Select JRE System library > Click on next > Click on Finish > Click on apply and Close. enter image description hereIt should work.... I hope so :)
右键单击您的包 > 导航到属性 > 单击 java 构建路径 > 单击库选项卡 > 单击添加库 > 选择 JRE 系统库 > 单击下一步 > 单击完成 > 单击应用并关闭。 在此处输入图像描述它应该可以工作......我希望如此:)
回答by Swaroop
Keep Messing with the options you get in eclipse , for example like try block for scanner and so on.. it will fix itself after a while. I don't know why this is happening.
继续搞乱你在 eclipse 中得到的选项,例如像 try 块用于扫描仪等等......它会在一段时间后自行修复。我不知道为什么会这样。