使用用户输入编写循环的Java

时间:2017-02-12 18:06:12

标签: java

/ **我正在尝试询问用户输入他们想要订购的书籍的数量,然后使用它来查找每本书的成本,总计它们并在最后为他们的订单提供收据。我知道如何给他们输出我的循环有问题。* /

import java.util.Scanner;

public class BookOrder {
    public static void main(String[] orgs){
        Scanner in = new Scanner(System.in);
        final double TAX = .065;
        final double SHIPPING = 2.95;
        int counter = 0;

        double bookSubtotal, subtotal, taxPaid;
        System.out.print("Please enter the  number of books you're ordering: ");
        double  numberOfBooks = in.nextDouble();
        for (counter = 0; counter < numberOfBooks; counter++){
            System.out.println("Please enter the cost of your book: ");
            double priceOfBooks = in.nextDouble();
            bookSubtotal = priceOfBooks + bookSubtotal;
            counter ++;

        }

        double subtotal = numberOfBooks * priceOfBooks;
        double taxpaid = subtotal * (TAX);
        double shippingCharge = SHIPPING * numberOfBooks;
        double sumOfOrder = bookSubtotal + priceOfOrder + shippingCharge + TAX;

            System.out.println("Number of books purchased:" + numberOfBooks);
            System.out.println("Book subtotal: $" + subtotal);
            System.out.println("Tax: $" + taxPaid);
            System.out.println("Shipping: $" + shippingCharge);
            System.out.println("-------------------------------");
            System.out.println("The price of the order is $" + sumOfOrder + ".");
    }
}

2 个答案:

答案 0 :(得分:0)

你似乎增加了两次计数器:

for (counter = 0; counter < numberOfBooks; counter++){
        System.out.println("Please enter the cost of your book: ");
        double priceOfBooks = in.nextDouble();
        bookSubtotal = priceOfBooks + bookSubtotal;
        counter ++;//this line

}

this line中发生的事情是你增加counter,但循环会为你做,因为:

for(counter = 0;counter<numberOfBooks;counter++)

该行中的counter++为您增加counter,所以只需删除

counter++;

在for循环中行(我写的那个this line旁边的那个)

此外,您应将值设置为bookSubtotal

int bookSubtotal = 0;

一开始。

此外,您可能希望numberOfBooks为整数:

int numberOfBooks = in.nextInt();

您不应该重新声明subtotal两次,只需删除该行中的double字样:

double subtotal = (double)numberOfBooks * priceOfBooks; 

在循环之前你也不需要创建taxpaid,因为你之后有taxPaid。命名区分大小写,意味着大写字母是ImpOrtaNt ......

答案 1 :(得分:0)

public class BookOrder {
    public static void main(String[] orgs){
        Scanner in = new Scanner(System.in);
        final double TAX = .065;
        final double SHIPPING = 2.95;
        int counter = 0;

        double bookSubtotal = 0;
        System.out.print("Please enter the  number of books you're ordering: ");
        int numberOfBooks = in.nextInt();
        for (counter = 0; counter < numberOfBooks; counter++){
            System.out.println("Please enter the cost of your book: ");
            double priceOfBooks = in.nextDouble();
            bookSubtotal += priceOfBooks;

        }

        double shippingCharge = SHIPPING * numberOfBooks;
        double tax = TAX * bookSubtotal;
        double sumOfOrder = bookSubtotal + shippingCharge + tax;
            System.out.println("Number of books purchased:" + numberOfBooks);
            System.out.println("Book subtotal: $" + bookSubtotal);
            System.out.println("Tax: $" + tax);
            System.out.println("Shipping: $" + shippingCharge);
            System.out.println("-------------------------------");
            System.out.println("The price of the order is $" + sumOfOrder + ".");
            }
}