这是我的问题:
我正在尝试使用Scanner和System.in从键盘获取输入并将其分配给int变量。
这就是我所拥有的(完整程序包含在下面):
// this program will use scanner objects and system inputs
public class Scanner
{
public static void main(String[] args)
{
int number;
// declares integer "number"
Scanner keyboard = new Scanner(System.in);
//-------------------------------------------------------------------
// declares referance variable (Scanner class) "keyboard", creates
// Scanner object that
// reads input from System.in, then assigns address of Scanner object
// to the reference variable "keyboard"
//-------------------------------------------------------------------
System.out.println("Enter an integer value: ");
// displays text
number = keyboard.nextInt();
// assigns keyboard input to "number" as integer value
System.out.println("You entered the integer " + number);
// displays "number" to see if the program worked
}
}
当我尝试使用javac在Windows命令提示符下编译程序时,出现此错误消息:
error: constructor Scanner in class Scanner
cannot be applied to given types;
Scanner keyboard = new Scanner(System.in);
required: no arguments
found: InputStream
reason: actual and formal argument lists differ in length
error: cannot find symbol
number = keyboard.nextInt();
^
symbol: method nextInt()
location: variable keyboard of type Scanner
2 errors
我做错了什么?
答案 0 :(得分:1)
有命名冲突。您必须从Java语言角度使Scanner
类成为唯一的,有两个选项可以做到:
让Scanner
班级明确
使用完全限定名称
public static void main(String[] args)
{
int number;
java.util.Scanner keyboard = new java.util.Scanner(System.in);
System.out.println("Enter an integer value: ");
number = keyboard.nextInt();
System.out.println("You entered the integer " + number);
}
答案 1 :(得分:0)
您应该重命名类Scanner - 错误在于,您使用Java标准库中名为Scanner的类。
答案 2 :(得分:0)
import java.util.Scanner;
public class Main {
public static void main(String[] args){
int number;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer value: ");
number = keyboard.nextInt();
System.out.println("You entered the integer " + number);
}
}