无法导入 java.util.Scanner
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35909160/
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
Can't import java.util.Scanner
提问by Anonymous
I have been trying to create program that lets a user input a number of eggs to buy and calculates the price. I have tried using the scanner method to input the integer, but I can't seem to import the scanner method.
我一直在尝试创建程序,让用户输入要购买的鸡蛋数量并计算价格。我曾尝试使用scanner方法输入整数,但我似乎无法导入scanner方法。
Here's what I have:
这是我所拥有的:
package eggsorder;
import java.util.Scanner;
import java.io.IOException;
public class EggsOrder {
static final double EGGS_DOZEN = 7.25;
static final double EGGS_SINGLE = 0.75;
static final int DOZEN_NUMBER = 12;
public static void main(String[] args) throws IOException {
System.out.println("Enter number of eggs for purchase: ");
Scanner enter = new Scanner(System.in);
int eggs = enter.nextInt();
System.out.println("You ordered " + eggs + "eggs.");
System.out.println("That is " + (eggs / DOZEN_NUMBER) + " dozen eggs at 7.25 per dozen and " + (eggs % DOZEN_NUMBER) + " additional eggs at 0.75 each");
System.out.println("Which is a total price of " + (((eggs % DOZEN_NUMBER) * EGGS_SINGLE) + ((eggs / DOZEN_NUMBER) * EGGS_DOZEN)));
This is the error I get after running:
这是我运行后得到的错误:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class Scanner
location: class java.util
at eggsorder.EggsOrder.<clinit>(EggsOrder.java:7)
The code works without the scanner method, but it needs to use it.
该代码无需扫描仪方法即可工作,但需要使用它。
I have tried using java.util and various other variations but to no avail.
我曾尝试使用 java.util 和其他各种变体,但无济于事。
Also, I am using the latest version of NetBeans and java
另外,我使用的是最新版本的 NetBeans 和 java
采纳答案by Vikrant Kashyap
I guess you are using an IDE (like Netbeans or eclipse) which allows you to run the code
even if certain classes
are not compilable
. During the application's runtime, if you access this class
it would lead to this exception.
我猜您正在使用 IDE(如 Netbeans 或 eclipse),它允许您运行code
即使某些classes
不是compilable
. 在应用程序的运行时,如果您访问class
它会导致此异常。
Solution :- Simply Clean Your Project and Build and Run Then Again.
解决方案:- 只需清理您的项目并构建并再次运行。
回答by Gopal00005
Make sure you have configured PATH, CLASSPATH and JAVA_HOME variable in system Environment variable.
1) It might refers older version of java then 1.5
or
2) May be not added PATH,CLASSPATH, JAVA_HOME variable there.
确保您已经在系统环境变量中配置了 PATH、CLASSPATH 和 JAVA_HOME 变量。
1) 它可能是指旧版本的 java 然后是 1.5
或
2) 可能没有在那里添加 PATH,CLASSPATH, JAVA_HOME 变量。
BTW Your code is works fine in my Eclipse.
顺便说一句,您的代码在我的 Eclipse 中运行良好。
回答by Rudolf
Try unchecking the "Compile on save" setting in Build -> Compiling.
尝试取消选中 Build -> Compiling 中的“Compile on save”设置。
回答by NullOverFlow
you can try this
你可以试试这个
java.util.Scanner enter = new java.util.Scanner(System.in);
i tested your code and it seems to import correctly on my end. Maybe something is wrong with your IDE try resetting it
我测试了你的代码,它似乎在我端正确导入。也许您的 IDE 有问题尝试重置它