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

    }

}

我的錯誤消息是:

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,所以如果您能幫助我解決這些錯誤,我將不勝感激。

絕對可以在項目中的某個地方使用Java文件名Scanner 嘗試

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

代替。 否則,編譯器將嘗試實例化您的類(也稱為Scanner)。

同樣,以大寫VendingMachine類的名稱也是個好習慣,應該使用VendingMachine而不是vendingMachine

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM