简体   繁体   English

无法在Eclipse中导入扫描器类

[英]Trouble importing scanner class in Eclipse

import java.util.Scanner;

public class vendingMachine {

    public static void main(String[] args) {
        final int PENNIES_PER_DOLLAR = 100;
        final int PENNIES_PER_QUARTER = 25;
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the bill ($ 1 is 1 $5 is 5, etc. : ");
        int billValue = in.nextInt();
        System.out.print("Please enter the price of the desired item in pennies. : ");
        int itemPrice = in.nextInt();

        // computation of change due 
        int changeDue = (billValue * PENNIES_PER_DOLLAR) - itemPrice  ;
        int dollarsBack = changeDue / PENNIES_PER_DOLLAR ;
        changeDue = changeDue % PENNIES_PER_DOLLAR; 
        int quartersBack = changeDue / PENNIES_PER_DOLLAR;

        // print statements 

        System.out.printf("Dollars due back %6d" , dollarsBack);
        System.out.println();
        System.out.printf("Quarters due back %6d" , quartersBack);
        System.out.println(); 

    }

}

My error messages are: 我的错误消息是:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor Scanner(InputStream) is undefined
The method nextInt() is undefined for the type Scanner
The method nextInt() is undefined for the type Scanner

at vendingMachine.main(vendingMachine.java:8)

I'm a coding newbie and Eclipse is the first IDE I have worked with, so I would appreciate it if you guys could help walk me through, resolving these errors. 我是一名编码新手,Eclipse是我使用的第一个IDE,所以如果您能帮助我解决这些错误,我将不胜感激。

Definitely you would have java file name Scanner somewhere in your project. 绝对可以在项目中的某个地方使用Java文件名Scanner Try 尝试

java.util.Scanner myScanner = new java.util.Scanner(System.in);

instead. 代替。 Else the compiler tries to instantiate your class which is also called Scanner. 否则,编译器将尝试实例化您的类(也称为Scanner)。

Also, it's a good practice to start the name of class with Upper Case, it should be VendingMachine instead of vendingMachine . 同样,以大写VendingMachine类的名称也是个好习惯,应该使用VendingMachine而不是vendingMachine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM