Я пишу программу, которая запрашивает у пользователя возраст и сумму денег, которые у них есть. Если они достигли совершеннолетия, они получат сообщение с подтверждением. Если они не будут, им будет отказано, и количество лет, оставшихся до 21, будет отображаться. То же самое касается денег. Это 5 долларов за пиво. Если у пользователя недостаточно, это скажет им, сколько им нужно. Однако ... Я не могу создать значение, отображающее 2 десятичных знака в ответе. Как я могу отобразить решение или сколько денег им нужно в виде суммы в долларах или (например, 1,00) вместо 1.0?Отобразите окончательное решение с отображением двух знаков после запятой
//import classes
import java.util.*;
public class beerLab
{
static Scanner console = new Scanner(System.in);
public static void main (String[] args)
{
// Declares the price of beer and the age required to purchase beer.
double beer = 5.00;
double moneyGiven;
int age = 21;
int ageGiven;
// Request the age from the client.
System.out.println("Please type customer age: ");
ageGiven = console.nextInt();
if (ageGiven < 21) // If the client is under 21 then their purchase will be denied.
{
System.out.println("No beer for you!" + "\n" + "Come back in " + (age - ageGiven) + " years" + "\n" + "Thank you for your patronage.");
return;
}
// Request the the amount of money the client has.
System.out.println("Type customer cash amount: ");
moneyGiven = console.nextDouble();
if (moneyGiven < beer) // If the client doesn't provide enough money, the program will tell client how much money they need.
{
System.out.println("Sorry, you need " + (beer - moneyGiven) + " more." + "\n" + "Thank you for your patronage.");
}
if (ageGiven > 21 & moneyGiven >= beer) // If the client is old enough and has enough money they can purchase the beer.
{
System.out.println("Congratulations, you can have some beer." + "\n" + "Thank you for your patronage.");
}
}
}
Возможный дубликат [формат Java валюты Number] (http://stackoverflow.com/questions/2379221/java-currency-number-format) –