我正在爲「班級書店」編寫程序。我覺得所有的東西都可以工作,但是對於發佈價格和標記,我得到一個「變量未初始化」的錯誤。然而,我給了他們兩個價值。我究竟做錯了什麼?我正在使用Netbeans。變量未初始化
import java.util.Scanner;
public class ProgrammingProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Set my variables for the program
double publisherPrice;
double newBook;
double usedBook;
double usedBookDiscount;
double rentalBook;
double rentalBookDiscount;
double markup;
int booksOrdered;
String bookName;
System.out.println("Enter the Book Title: ");
bookName = keyboard.next();
System.out.println("Enter the Amount of Books Ordered: ");
booksOrdered = keyboard.nextInt();
//Set the conditions for markup
if (booksOrdered < 20) {
markup = .40;
}
if (booksOrdered >= 20 && booksOrdered < 50){
markup = .35;
}
if (booksOrdered >= 50 && booksOrdered < 100){
markup = .30;
}
if (booksOrdered >=100){
markup = .25;
}
//Set calculations for program to use
usedBookDiscount = .75;
rentalBookDiscount = .40;
newBook = (publisherPrice + markup);
usedBook = ((newBook * usedBookDiscount) - usedBookDiscount);
rentalBook = ((newBook * rentalBookDiscount - rentalBookDiscount));
System.out.println(bookName + publisherPrice + newBook + usedBook + rentalBook);
}
}
錯誤說明全部 – Reimeus
該錯誤消息是不言自明的。我沒有看到任何代碼給'publisherPrice'和'markup'任何值。 – tbodt
我沒有意識到我沒有要求輸入publishingPrice,但我的錯誤。 –