Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

java - How would i program this correctly?

what the program wants me to code:

Code an executable program that will produce an invoice for a customer ordering a number of products at a store. A sample run of the program is shown to the right.

Your program must ask for the number of products (up to a maximum of 12 products may be ordered) and then successively ask for the product name and its cost. The invoice produced includes:

? the title of the store (as shown), ? product names and their cost, ? calculated cost of all products, ? calculated 5% sales tax, ? overall total cost ? a thank you.

The products and their cost must be kept in parallel arrays. Two methods must be coded. One method will display the title. The second method will accept the calculated cost of all products and return the calculated sales tax. The method that computes the sales tax must use a named constant for the 5% tax rate.

picture of example run of what it should look like: http://imgur.com/F3XDjau

Currently my program is this so far, but im not sure if it is correct or if i need to make the variables into an array.

    public static void main(String[] args) {
        Scanner input= new Scanner(System.in);
        int product;
        String products;
        double cost;

        System.out.println("How many products? ");
        product=input.nextInt();

        for(int i = 0; i < product; i++){

            System.out.println("Product Name: ");
            products=input.next();

            System.out.println("Cost: ");
            cost=input.nextDouble();
        }

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this is how you can fill your array:

double[] costArray = new double[product]; 
for(int i = 0; i < product; i++){
    costArray[i] = input.nextDouble();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...