有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何正确编程?

程序希望我编写的代码:

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.

它应该是什么样子的示例运行图片:http://imgur.com/F3XDjau

目前为止,我的程序是这样的,但我不确定它是否正确,或者我是否需要将变量放入数组中

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

        }
    }

共 (2) 个答案

  1. # 1 楼答案

    您需要为变量产品成本使用一个数组,如下所示:

     static final float TAXES = 0.05f;
    
    public static void main(String[] args) {
        double sum = 0.0;
        double tax;
        Scanner input = new Scanner(System.in);
        int product;
        String products[];
        double cost[];
    
        System.out.println("How many products? ");
        product = input.nextInt();
        products = new String[product];
        cost = new double[product];
    
        for (int i = 0; i < product; i++) {
    
            System.out.println("Product Name: ");
            products[i] = input.next();
    
            System.out.println("Cost: ");
            cost[i] = Double.parseDouble(input.next().trim().replace(',', '.'));
        }
    
        indentedText();
    
        for (int i = 0; i < product; i++) {
            System.out.printf(products[i] + '\t' + "%.2f %n", cost[i]);
            sum = sum + cost[i];
        }
        tax = calculateTaxes(sum);
        System.out.printf("Sub total:" + '\t' + "%.2f %n", sum);
        System.out.printf("Sales tax:" + '\t' + "%.2f %n", tax);
        System.out.printf("Total to be paid:" + '\t' + "%.2f %n %n", (sum + tax));
        System.out.print('\t' + "Thank you!");
    
    }
    
    private static void indentedText() {
        System.out.print('\t' + "The Company Store" + '\n' + '\n');
    
    }
    
    private static double calculateTaxes(double sum) {
        return sum * TAXES;
    }
    
  2. # 2 楼答案

    以下是填充阵列的方法:

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