';无效';此处不允许输入错误 import java.util.Scanner; 导入java.util.ArrayList; 公共类网上商店{ 字符串[]书籍={ “Java简介”、“C++简介”、“C++简介”、“Perl” }; 字符串[]DVD={ “白雪公主”、“灰姑娘”、“小飞象”、“斑比” }; 双倍价格={ 45.99, 89.34, 100.00, 25.0 }; 双[]dvdsPrices={ 19.99, 24.99, 17.99, 21.99 }; ArrayListcartItems=new ArrayList(); ArrayList价格=新ArrayList(); java.util.Scanner input=新扫描仪(System.in); int用户选择; 公共void显示菜单(){ System.out.printf(“**欢迎来到野马图书和DVD商店**/n/n”+”从以下选项中选择:/n1-浏览图书库存“+”并将一本书添加到购物车/n2-浏览DVD库存并将“+”一张DVD添加到购物车/n3-查看购物车/n4-结帐/n9-下单“+”并退出商店”); } 公共静态void displayArray(String[]itemsArray,double[]pricesArray,String itemtype){ System.out.printf(“库存编号项目类型价格”+”-------------------------------------------------“+”1“+项目数组[0]+价格数组[0]+”2“+项目数组[1]+价格数组[1]+”3“+项目数组[2]+价格数组[2]+”4“+项目数组[3]+价格数组[3]); } 公共无效getTotal(ArrayList价格){ 双倍合计=0; 双重征税总额; 双重征税; int i; 对于(i=0;i

';无效';此处不允许输入错误 import java.util.Scanner; 导入java.util.ArrayList; 公共类网上商店{ 字符串[]书籍={ “Java简介”、“C++简介”、“C++简介”、“Perl” }; 字符串[]DVD={ “白雪公主”、“灰姑娘”、“小飞象”、“斑比” }; 双倍价格={ 45.99, 89.34, 100.00, 25.0 }; 双[]dvdsPrices={ 19.99, 24.99, 17.99, 21.99 }; ArrayListcartItems=new ArrayList(); ArrayList价格=新ArrayList(); java.util.Scanner input=新扫描仪(System.in); int用户选择; 公共void显示菜单(){ System.out.printf(“**欢迎来到野马图书和DVD商店**/n/n”+”从以下选项中选择:/n1-浏览图书库存“+”并将一本书添加到购物车/n2-浏览DVD库存并将“+”一张DVD添加到购物车/n3-查看购物车/n4-结帐/n9-下单“+”并退出商店”); } 公共静态void displayArray(String[]itemsArray,double[]pricesArray,String itemtype){ System.out.printf(“库存编号项目类型价格”+”-------------------------------------------------“+”1“+项目数组[0]+价格数组[0]+”2“+项目数组[1]+价格数组[1]+”3“+项目数组[2]+价格数组[2]+”4“+项目数组[3]+价格数组[3]); } 公共无效getTotal(ArrayList价格){ 双倍合计=0; 双重征税总额; 双重征税; int i; 对于(i=0;i,java,compiler-errors,Java,Compiler Errors,在第59行,我得到了错误“'void'type not allowed”。我真的不知道如何解决这个问题,如果能得到一些帮助,我将不胜感激。我理解虚空是如何无法返回的,但我有点不明白为什么会发生这种情况。这就是问题所在getTotal方法的类型为void,您正在尝试打印它。更改返回类型或删除打印语句 import java.util.Scanner; import java.util.ArrayList; public class OnlineStore { String[] books

在第59行,我得到了错误“'void'type not allowed”。我真的不知道如何解决这个问题,如果能得到一些帮助,我将不胜感激。我理解虚空是如何无法返回的,但我有点不明白为什么会发生这种情况。

这就是问题所在
getTotal
方法的类型为void,您正在尝试打印它。更改返回类型或删除打印语句

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

这就是问题所在
getTotal
方法的类型为void,您正在尝试打印它。更改返回类型或删除打印语句

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

这句话的意思是:

System.out.print("---------------------- \n" +
                "Total + tax" + getTotal(prices));
出现错误的原因是getTotal没有返回任何内容

System.out.print("---------------------- \n" +
                "Total + tax" + getTotal(prices));
public void getTotal(数组列表价格){
如果希望getTotal返回某些内容,可以将签名更改为

public void getTotal(ArrayList <Double> prices) {
public Double getTotal(数组列表价格){

然后适当地调整函数的结尾。(添加返回语句)

所讨论的行是:

System.out.print("---------------------- \n" +
                "Total + tax" + getTotal(prices));
出现错误的原因是getTotal没有返回任何内容

System.out.print("---------------------- \n" +
                "Total + tax" + getTotal(prices));
public void getTotal(数组列表价格){
如果希望getTotal返回某些内容,可以将签名更改为

public void getTotal(ArrayList <Double> prices) {
public Double getTotal(数组列表价格){

然后适当地调整函数的结尾。(添加一个return语句)

哪一行是第59行?通常这意味着您试图打印一个不返回任何内容的方法:
System.out.println(someMethod())
其中someMethod已声明为返回
void
。解决方案:不要这样做。要么让该方法返回字符串或对象,要么不要尝试将其打印出来。这不可能是仍然显示问题的最小示例。您正在尝试打印
getTotal(prices)的结果
返回
void
,所以这没有多大意义。是的,没有
getTotals()
在其中打印任何内容。它是一个getter方法,因此应该返回一些内容,这就是它应该做的。因此,从
getTotals()
中删除所有println,让它返回总数。将来,看看car