import java.util.Scanner;
import java.util.ArrayList;
public class OnlineStore {
String[] books = {
"Intro to Java", "Intro to C++", "Intro to C++", "Perl"
};
String[] dvds = {
"Snow White", "Cinderella", "Dumbo", "Bambi"
};
double[] booksPrices = {
45.99, 89.34, 100.00, 25.0
};
double[] dvdsPrices = {
19.99, 24.99, 17.99, 21.99
};
ArrayList < String > cartItems = new ArrayList < > ();
ArrayList < Double > prices = new ArrayList < > ();
java.util.Scanner input = new Scanner(System. in );
int userChoice;
public void displayMenu() {
System.out.printf("**Welcome to the Mustangs Books and DVDs Store**/n/n" + "Choose from the following options:/n1 - Browse books inventory" + "and add a book to the cart/n2 - Browse DVDs inventory and add" + "a DVD to the cart/n3 - View Cart/n4 - Checkout/n9 - Canel order" + "and exit store");
}
public static void displayArrays(String[] itemsArray, double[] pricesArray, String itemtype) {
System.out.printf("Inventory Number itemType Prices" + "------------------------------------------" + " 1" + itemsArray[0] + pricesArray[0] + " 2" + itemsArray[1] + pricesArray[1] + " 3" + itemsArray[2] + pricesArray[2] + " 4" + itemsArray[3] + pricesArray[3]);
}
public void getTotal(ArrayList < Double > prices) {
double total = 0;
double taxTotal;
double tax;
int i;
for (i = 0; i < prices.size(); i++) {
total += prices.get(i);
}
tax = total * .825;
taxTotal = tax + total;
System.out.print("Your total is " + taxTotal);
}
public int getInventoryNumber() {
System.out.print("Enter the inventory number you wish to purchase or enter -1 if you wish to see the menu: ");
userChoice = input.nextInt();
if (userChoice == -1) {
displayMenu();
}
int correctNumber = userChoice - 1;
return correctNumber;
}
public String displayArrays(ArrayList < String > items, ArrayList < Double > prices) {
int i;
System.out.print("Items Prices" + "----------------------");
for (i = 0; i < items.size(); i++)
System.out.printf("%d%s%f", i, items.get(i), prices.get(i));
System.out.print("---------------------- \n" + "Total + tax" + getTotal(prices));
}
public void addToCart(int inventoryNumber, ArrayList < String > cart, String[] inventory) {
cart.add(inventory[inventoryNumber]);
}
public void cancelOrder(ArrayList < String > cartItems, ArrayList < Double > prices) {
cartItems.clear();
prices.clear();
}
public void main(String[] args) {
int userInput;
do {
displayMenu();
userInput = input.nextInt();
if (userInput != 1 && userInput != 2 && userInput != 3 && userInput != 4 && userInput != 9) {
System.out.println("This option is not acceptable.");
} else if (userInput == 1) {
displayArrays(books, booksPrices, "books");
int correctNumber = getInventoryNumber();
addToCart(correctNumber, cartItems, books);
} else if (userInput == 2) {
displayArrays(dvds, dvdsPrices, "dvds");
getInventoryNumber();
int correctNumber = getInventoryNumber();
addToCart(correctNumber, cartItems, dvds);
} else if (userInput == 3) {
displayArrays(cartItems, prices);
} else if (userInput == 4) {
getTotal(prices);
} else {
cancelOrder(cartItems, prices);
}
}
while (userInput != 9);
}
}
在第 59 行,我收到错误“'void' 类型不允许”。我不太确定如何解决这个问题,希望得到一些帮助。我理解空虚无法有返回,但我有点不明白为什么会发生这种情况。
最佳答案
有问题的行是这一行:
System.out.print("---------------------- \n" +
"Total + tax" + getTotal(prices));
您收到错误的原因是 getTotal 没有返回任何内容。
public void getTotal(ArrayList <Double> prices) {
如果您希望 getTotal 返回某些内容,您可以将签名更改为..
public Double getTotal(ArrayList <Double> prices) {
然后适当调整函数的结尾。 (添加返回语句)
关于java - 此处不允许出现“Void”类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445334/