price.java
来自「java源程序 对初学者有很大的帮助 从简单到复杂」· Java 代码 · 共 44 行
JAVA
44 行
//********************************************************************
// Price.java Author: Lewis/Loftus
//
// Demonstrates the use of various Keyboard and NumberFormat
// methods.
//********************************************************************
import cs1.Keyboard;
import java.text.NumberFormat;
public class Price
{
//-----------------------------------------------------------------
// Calculates the final price of a purchased item using values
// entered by the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final double TAX_RATE = 0.06; // 6% sales tax
int quantity;
double subtotal, tax, totalCost, unitPrice;
System.out.print ("Enter the quantity: ");
quantity = Keyboard.readInt();
System.out.print ("Enter the unit price: ");
unitPrice = Keyboard.readDouble();
subtotal = quantity * unitPrice;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax;
// Print output with appropriate formatting
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
NumberFormat fmt2 = NumberFormat.getPercentInstance();
System.out.println ("Subtotal: " + fmt1.format(subtotal));
System.out.println ("Tax: " + fmt1.format(tax) + " at "
+ fmt2.format(TAX_RATE));
System.out.println ("Total: " + fmt1.format(totalCost));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?