当前位置: 首页 > 知识库问答 >
问题:

请帮我解决简单的Java程序

云伯寅
2023-03-14

任务是:

编写一个程序,为在XYZ书店购买任意两本书的会员提供20%的折扣。(提示:将常量变量用于20%的折扣。)

我已经做了编码,但是不能提示书名,然后显示折扣价,请在下面看到我的编码,根据需要修改。

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

}

共有1个答案

曹新觉
2023-03-14

为了提示书名,你可以写这样的东西:

/* 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);
 类似资料:
  • 错误: log4j:WARN找不到记录器(org.hibernate.cfg.annotations.Version)的appender。log4j:警告请正确初始化log4j系统。线程“main”组织中出现异常。冬眠例外GenericJDBCException:无法在组织上打开连接。冬眠例外SQLStateConverter。在org上处理非特定异常(sqlstatecoverter.java:

  • 我正试图解决这个问题:

  • 如果我输入字母,下面的程序不应该崩溃(错误粘贴在底部);相反,它应该要求用户输入一个有效的数字。 **输出#1//当用户输入时,句号为abc 输出#2//当年份数据类型为字节,但用户输入300,而不是崩溃时,程序应要求用户输入有效数字

  • 请查找我的nginx日志输出(access.log) 182.69.143.120---[31/Mar/2016:03:10:51 0530]“GET/api/check_delivery.php?authkey=9450AW2qTKYe453be757c My nginx自定义日志格式: $remote_addr-$remote_user[$time_local]“$request”$host'

  • 我有这样的家庭作业问题: 以下数据代表地震的里氏震级数据。编写一个程序来计算和打印任何有效地震数据的平均值。 将里氏震级值存储在名为quakeLevels的双精度数组中。 不幸的是,你的地震仪有时会产生不可靠的读数(如本例中的10.1)。所以你决定去掉最大和最小读数。 您的程序应该执行以下操作: 使用以下数据声明并初始化quakeLevels数组。{ 5.6, 6.2, 4.0, 5.5, 5.7