这是我的任务:
编写一个模拟收银机的程序。提示用户输入三件商品的价格。将它们加在一起以获得小计。确定小计的税 (6%)。找到销售小计加税的总金额。显示每个项目的价格、小计金额、税额和最终金额。
我这样做了:
package cashregisteremulator;
import java.util.Scanner;
public class CashRegisterEmulator {
public static void main(String[] args) {
Scanner price = new Scanner(System.in);
System.out.print("Please enter a price for item number one $");
double price1 = price.nextDouble();
System.out.print("Please enter a price for item number two $" );
double price2 = price.nextDouble();
System.out.print("Please enter a price for item number three $");
double price3 = price.nextDouble();
double total = ((price1) + (price2) + (price3));
System.out.println("The subtotal is $" + total);
double tax = .06;
double totalnotax = (total * tax );
System.out.println("The tax for the subtotal is $" + totalnotax);
double totalplustax = (total + totalnotax);
System.out.println("The total for your bill with tax is $" + totalplustax);
}
}
但是,我需要使用循环来完成这项任务。由于提示只要求 3 次迭代,我想到了使用 for 循环。到目前为止,我有这个:
package CashRegister;
import java.util.Scanner;
public class CashRegister {
public static void main(String[] args) {
Scanner askPrice = new Scanner(System.in);
for(double i = 0 ; i < 3; i++);
{
System.out.println("Enter a value") ;
double price = askPrice.nextDouble();
}
}
}
我要怎么做才能向用户索要三倍的价格?