Налог с продаж в размере 7% взимается со всех потребляемых товаров и услуг. Также обязательно, чтобы на всех ценниках был указан налог с продаж. Например, если цена товара составляет 107 долларов США, фактическая цена равна 100 долларам США, а 7 долларов США идут на налог с продаж.
Напишите программу, использующую цикл для непрерывного ввода данных. цена с учетом налога (как «двойная»); рассчитать фактическую цену и налог с продаж (в «двойном» размере); и распечатайте результаты, округленные до 2 десятичных знаков. Программа завершится в ответ на ввод -1; и выведите общую цену, общую фактическую цену и общий налог с продаж.
Однако, когда я пытаюсь вычислить налог с продаж, вместо того, чтобы показать это:
Введите·цену·с·налогом·в·долларах· (или·-1·до·конца): 107
Фактическая·цена: 100,00 долларов США
Налог с продаж: 7,00 долларов США
Мои расчеты показывают это :
Введите цену с учетом налога в долларах (или -1 до конца): 107
Фактическая цена составляет 99,51 доллара США.
Налог с продаж составляет 7,49 доллара США.
Я не уверен, что не так с моим кодом.
Код: Выделить всё
import java.util.Scanner;
public class SalesTax{
public static void main(String[] args) {
// Declare constants
final double SALES_TAX_RATE = 0.07;
final int SENTINEL = -1; // Terminating value for input
// Declare variables
double price, actualPrice, salesTax; // inputs and results
double totalPrice = 0.0, totalActualPrice = 0.0, totalSalesTax = 0.0; // to accumulate
// Read the first input to "seed" the while loop
Scanner in = new Scanner(System.in);
System.out.print("Enter the tax-inclusive price in dollars (or -1 to end): ");
price = in.nextDouble();
while (price != SENTINEL) {
// Compute the tax
salesTax = SALES_TAX_RATE * price;
actualPrice = price - salesTax;
// Accumulate into the totals
totalPrice = actualPrice + salesTax;
totalActualPrice = actualPrice + actualPrice;
totalSalesTax = salesTax + salesTax;
// Print results
System.out.println("Actual price is $" + String.format("%.2f",actualPrice));
System.out.println("Sales Tax is: $" + String.format("%.2f",salesTax));
// Read the next input
System.out.print("Enter the tax-inclusive price in dollars (or -1 to end): ");
price = in.nextDouble();
// Repeat the loop body, only if the input is not the sentinel value.
// Take note that you need to repeat these two statements inside/outside the loop!
}
// print totals
System.out.println("Total price is: " + String.format("%.2f",totalPrice));
System.out.println("Total Actual Price is: " + String.format("%.2f",totalActualPrice));
System.out.println("Total sales tax is: " + String.format("%.2f",totalSalesTax));
}
}
Подробнее здесь: https://stackoverflow.com/questions/600 ... -the-price
Мобильная версия