Въпрос за задача от изпит
Здравейте,
Искам да ви попитам, защо на следната задача изкарвам 80 точки. Пиша задачата на Java.
Условието на задачата:
Problem, solution and tests - Hungry Garfield
package hungrygarfield;
import java.util.Scanner;
public class HungryGarfield {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double money = input.nextDouble();
double rate = input.nextDouble();
double pizzaPrice = input.nextDouble();
double lasagnaPrice = input.nextDouble();
double sandwichPrice = input.nextDouble();
double pizzaQuantity = input.nextDouble();
double lasagnaQuantity = input.nextDouble();
double sandwichQuantity = input.nextDouble();
pizzaPrice = pizzaPrice/rate;
lasagnaPrice = lasagnaPrice/rate;
sandwichPrice = sandwichPrice/rate;
double sumGarfieldNeeds = (pizzaPrice*pizzaQuantity)+(lasagnaPrice*lasagnaQuantity) +(sandwichPrice*sandwichQuantity) ;
if (money >= sumGarfieldNeeds) {
System.out.printf("Garfield is well fed, John is awesome. Money left: $%.2f.%n",(money-sumGarfieldNeeds));
}
else {
System.out.printf("Garfield is hungry. John is a badass. Money needed: $%.2f.%n",(sumGarfieldNeeds-money));
}
}
}
Аз използвам Java и незнам дали е възможно да се използва decimal
BigDecimal
Опитай:
" use a
float
(instead ofdouble
) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead "