运行我的代码时throwFor,next和nextDouble错误

时间:2019-02-19 18:37:48

标签: java

运行代码时出现一些错误。我不明白为什么我才刚刚开始收到这些,但我非常感谢您的帮助。我的代码是一家商店,询问该人两种类型的糖果,它们的价格,然后询问消费者他们想要购买多少盒。输入完糖果的前两个名称后,错误就会出现,看起来像这样:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at Program3Main.main(Program3Main.java:27)

非常感谢您的帮助!

public class Program3Main {

    public static void main(String args[]) 
    {

        System.out.println("          Coulter's Candy Shop\n");

        // This is the retailer section \\
        System.out.println("--------------- Retailer ---------------\n");

        // This sub-section asks the user to enter the first candies name and price.
        System.out.println("Please enter the name of the first candy: ");
        Scanner s= new Scanner(System.in); // This is the scanner that will collect information.
        String candy1name=s.next();

        System.out.println("Please enter how much the first candy costs: ");
        double candy1price=s.nextDouble();


        // This sub-section asks the user to enter the second candies name and price.
        System.out.println("Please enter the name of the second candy: ");
        String candy2name=s.next();

        System.out.println("Please enter how much the second candy costs: ");
        double candy2price=s.nextDouble();
        System.out.println("");


        // This is the consumer section \\
        System.out.println("--------------- Consumer ---------------\n");
        System.out.println("Any 13 or more boxes of the same candy is sold at a 5% discount. (We also apply a 6.5% sales tax to your boxes of candy.");
        System.out.println("");

        System.out.println("How many boxes of " + candy1name + " would you like to purchase?");
        int numCandy1=s.nextInt(); // Takes user input for # of candy 1.

        System.out.println("How many boxes of " + candy2name + " would you like to purchase?");
        int numCandy2=s.nextInt(); // Takes user input for # of candy 2.
        System.out.println("");


        System.out.println("---- Candy Units Price / Units Cost ----\n");

        // Variables for candy 1
        double moneySavedCandy1;
        double totalCostCandy1;
        double totalCostCandy1p1;
        double totalCostCandy1p2;
        double taxCostCandy1;

        // If and else statements to make sure candy 1 isn't above 12 (Determine what equation to exe) \\
        if (numCandy1 <= 12)
        {       
            taxCostCandy1 = (candy1price * numCandy1) * 0.0625; // Calculates sales tax
            totalCostCandy1 = (candy1price * numCandy1); // Calculates final total

            System.out.println("Your boxes of " + candy1name + " cost $" + totalCostCandy1);

        }
        else if (numCandy1 > 12)// Else to calculate 5% discount on any candy amount > 12
        {
            totalCostCandy1p1 = 12 * candy1price;
            totalCostCandy1p2 = (numCandy1 - 12) * (candy1price * .95 );
            taxCostCandy1 = (totalCostCandy1p1 + totalCostCandy1p2) * 0.0625; // Calculates sales tax
            totalCostCandy1 = (totalCostCandy1p1 + totalCostCandy1p2) + taxCostCandy1; // Calculates final total

            moneySavedCandy1 = (numCandy1-12) * (candy1price * .5);
            System.out.println("Your boxes of " + candy1name + " cost $" + totalCostCandy1);
            System.out.println("You saved $" + moneySavedCandy1 + "!");
            System.out.println("");
        }
        else 
        {
            totalCostCandy1 = 0;
            taxCostCandy1 = 0;
        };


        // Variables for candy 2
        double totalCostCandy2p1;
        double totalCostCandy2p2;
        double totalCostCandy2;
        double moneySavedCandy2;
        double taxCostCandy2;

        // If and else statements to make sure candy 2 isn't above 12 (Determine what equation to exe) \\
        if (numCandy2 <= 12)
        {       
            taxCostCandy2 = (candy2price * numCandy2) * 0.0625; // Calculates sales tax
            totalCostCandy2 = (candy2price * numCandy2); // Calculates final total

            System.out.println("Your boxes of " + candy2name + " cost $" + totalCostCandy2);

        }
        else if (numCandy2 > 12)// Else to calculate 5% discount on any candy amount > 12
        {
            totalCostCandy2p1 = 12 * candy2price;
            totalCostCandy2p2 = (numCandy2 - 12) * (candy2price * .95 );
            taxCostCandy2 = (totalCostCandy2p1 + totalCostCandy2p2) * 0.0625; // Calculates sales tax
            totalCostCandy2 = (totalCostCandy2p1 + totalCostCandy2p2) + taxCostCandy2; // Calculates final total

            moneySavedCandy2 = (numCandy2-12) * (candy2price * .5);
            System.out.println("Your boxes of " + candy2name + " cost $" + totalCostCandy2);
            System.out.println("You saved $" + moneySavedCandy2 + "!");
            System.out.println("");
        }
        else 
        {
            totalCostCandy2 = 0;
            taxCostCandy2 = 0;
        };

        double totalOrder;
        double totalTax;
        totalTax = (taxCostCandy1 + taxCostCandy2); // Calculates the total tax
        totalOrder = totalCostCandy1 + totalCostCandy2 + totalTax; // Calculates the entire order total
        System.out.println("The total tax on your order is: $" +totalTax);
        System.out.println("Your orders total is: $" + totalOrder);

    }
}

1 个答案:

答案 0 :(得分:1)

我打赌您会两次ENTER,因为您希望光标移到控制台的下一行,而Scanner#next不会发生。

只需从next()切换到nextLine()
还请记住,使用nextDouble()时不能在实际数字之前添加空格。