gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:49:38 25 4
gpt4 key购买 nike

当我运行以下代码时,它显示扫描仪无法解析为键入的错误。我检查了 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)学生上方,您需要添加:

import java.util.Scanner;

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com