This is a simple code which ask you to enter how much money do you have, and then you should enter the name of the products you wish to buy and these products prices in one line.
then the program is supposed to tell you if you have enough money to buy these products or you don't and also print the product with the lowest price.
Example:
enter amount of money you have:
100
enter products you want to buy and the value for each:
Apple 10
orange 20
output of the code:
you have enough money
the lowest price is (Apple 10)
I have 2 problems with this code
1- first when i try to stop the scanner from taking inputs I'm supposed to enter "stop" as an input but in my case the action is done only if I enter "stop" 2 times, don't know why?
2- I need to determine the minimum product value and print it and I Have Tried a lot of codes but non of them worked with me.
this is my code
Scanner input = new Scanner (System.in);
String productName="";
double totalPrice=0;
double productValue = 0;
System.out.println("How much money do you have? ");
double money = input.nextDouble();
System.out.println("please insert the items in the invoice (the name of product and its price): "
+ " insert \"stop\" as the name of the product to finish your input");
while (!(productName.equals("stop")) ){
if(input.hasNext()){ productName = input.next();}
if (input.hasNextDouble()){ productValue = input.nextDouble();}
totalPrice = totalPrice + productValue;
}
if (money > totalPrice ){
System.out.println("you have enough money");}
else {
System.out.println("you don't have enough money");}
}
Aucun commentaire:
Enregistrer un commentaire