我是 java 和 netbeans 新手。我正在尝试编写一个需要用户输入的程序。这是我的代码:
public class Arrays {
public static void main(String[] args){
}
private double[] readNumbers(){
final Input in = new Input();
System.out.print("How many numbers will you enter?: ");
final int count = in.nextInt();
final double[] list = new double[count];
for (int i = 0; i < count; ++i){
System.out.print("Enter next number: ");
list[i] = in.nextDouble();
}
return list;
}
}
在 final Input in - new Input();
行中,Netbeans 在 Input 下划线表示它找不到符号。然而,我实际上是从教科书上复制了这段代码,所以我不明白问题是什么。我想也许我需要import java.io
,但这并没有解决问题。如果这是一个愚蠢的问题,我真的很抱歉,但我们将不胜感激。
谢谢!
最佳答案
看起来您的教科书上有一些您忘记导入到项目中的类定义。 如果您愿意,请更改您的代码,例如,
final Input in = new Input();
到
final Scanner in = new Scanner(System.in);
如果您不想更改代码,请在获得此代码的页面上上下下查看几页,您应该会看到名为 Input 的类,与此类似:
class Input{
public int nextInt(){
Scanner sc=new Scanner(System.in);
return sc.nextInt();
}
public double nextDouble(){
Scanner sc=new Scanner(System.in);
return sc.nextDouble();
}
}
这基本上是一项额外的不必要的工作。 将其包含在您的项目中,它应该运行良好。
关于java - 在java中创建输入对象时找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12906165/