java - 尝试使用扫描仪时出现 "Cannot be resolved to a type"

标签 java eclipse java.util.scanner

当我运行以下代码时,它显示扫描仪无法解析为类型的错误。我检查了 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() ;
}
}
}

最佳答案

您还需要导入类本身。在文件的最顶部,public class student 之上,您需要添加:

import java.util.Scanner;

此外,我想再提出一些可能的更正:

  • 类名应该是 PascalCase
  • 您的代码应该有一致的缩进。 Ctrl+Shift+F 是你的 friend 。

关于java - 尝试使用扫描仪时出现 "Cannot be resolved to a type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819174/

相关文章:

java - 在 .before() 中传递 int 以计算它是否早于 x 天

android - 如何从 Eclipse 中删除 'unremovable' 控制台

eclipse galileo 无法在 mac 上发布到 tomcat!

java - 使用扫描仪获取 NoSuchElementFound

java - 无限循环 - Scanner.hasNextInt() 在 while 循环内

java - 在 JNA 中定义 HGlobal

java - ConcurrentHashMap 与 HashMap 和 NullPointerException

java - 23/12/2013 是用 MM/dd/yyyy 格式映射的,为什么,为什么不是 ParseException

eclipse - 如何在 Eclipse 中将项目从 Jsf 2.2 升级到 Jsf 2.3

java - 使用 Scanner 读取 utf-8