简体   繁体   English

请帮我解决简单的Java程序

[英]Please Help me to solve the Simple Java program

The assignment is: 分配是:

Write a program that provides 20% discount for member who purchase any two books at XYZ bookstore. 编写一个程序,为在XYZ书店购买任意两本书的会员提供20%的折扣。 (Hint: Use constant variable to the 20% discount.) (提示:使用常数变量可享受20%的折扣。)

I have done the coding, but cannot prompt book name, and then show the discounted price. 我已经完成了编码,但是无法提示书名,然后显示折扣价。 Please see my coding below and modify it as your needs. 请在下面查看我的编码,并根据需要进行修改。

import java.util.Scanner;

public class Book_Discount {
  public static void main(String args[]) {
  public static final double d = 0.8;
  Scanner input = new Scanner(System.in);

  int purchases;
  double discounted_price;
  System.out.print("Enter value of purchases: ");

  purchases = input.nextInt();
  discounted_price = purchases * d; // Here discount calculation takes place

  // Displays discounted price
  System.out.println("Value of discounted price: " + discounted_price); 
  }

}

For prompting the book name as well, you write something like: 为了同时提示书名,您可以编写以下内容:

/* Promt how many books */
System.out.print("How many books? ");
int bookCount = scanner.nextInt();
scanner.nextLine(); // finish the line...
double totalPrice = 0.0d; // create a counter for the total price

/* Ask for each book the name and price */
for (int i = 0; i < bookCount; ++i)
{
    System.out.print("Name of the book? ");
    String name = scanner.nextLine();  // get the name
    System.out.print("Price of the book? ");
    double price = scanner.nextDouble(); // get the price
    scanner.nextLine(); // finish the line
    totalPrice += price; // add the price to the counter
}

/* If you bought more than 1 book, you get discount */
if (bookCount >= 2)
{
    totalPrice *= 0.8d;
}

/* Print the resulting price */
System.out.printf("Total price to pay: %.2f%n", totalPrice);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM