无法在Eclipse中导入扫描程序类

时间:2015-06-30 20:13:26

标签: java eclipse import java.util.scanner importerror

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(); 

    }

}

我的错误消息是:

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)

我是一个编码新手,Eclipse是我使用过的第一个IDE,所以如果你们能帮助我解决这些错误,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

你的项目中某处肯定会有java文件名Scanner。尝试

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

代替。否则编译器会尝试实例化您的类,也称为Scanner。

此外,使用大写字母启动类的名称是一个好习惯,它应该是VendingMachine而不是vendingMachine