1
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我合作過,所以如果你們能我將不勝感激幫助引導我,解決這些錯誤。感謝您的幫助提前。
你是否也有自己的班級,名爲Scanner?如果是這樣,你可能會混淆編譯器。我注意到這個消息並沒有把它稱爲'java.util.Scanner',而是簡單的'Scanner',這就是我爲什麼想知道這個。 –
你保存這門課的名字是什麼?你確定你把它命名爲vendingMachine.java嗎? –
您必須創建一個名爲Scanner的類或其他一些命名錯誤。你能向我們展示一個你的包瀏覽器結構的圖片嗎? – DrZoo