In: Computer Science
in java
my code
double price[] = {26.99, 22.99, 13.99, 56.99, 38.99};
// Variable Declaration
Scanner keyIn = new Scanner(System.in);
//Header
System.out.println( "----------------------------------------\n"
+ " Grocery Shop Price Calculator \n"
+ "----------------------------------------\n") ;
System.out.print ("Please enter the quantities for each item in the list? ");
double totalCost = 0;
double fishAmount = 0;
for(int i=0; i<price.length; i++)
{
int items = Integer.parseInt(keyIn.nextLine());
if(i==4)
fishAmount = price[i]*items;
else
totalCost = totalCost + price [i]*items;
}
System.out.print("Do you have the membership(Y/N) ");
String st = keyIn.nextLine();
double off;
if(totalCost<250)
off = totalCost*0.1;
else if(totalCost<500)
off = totalCost*0.2;
else
off = totalCost*0.15;
totalCost = totalCost + fishAmount - off;
long points = 0;
if(st.equalsIgnoreCase("Y"))
{
if(totalCost<500)
points = Math.round(totalCost*2);
else
points = Math.round(totalCost*3);
}
System.out.print("The total price is $"+ totalCost+".");
if(st.equalsIgnoreCase("Y"))
System.out.println("You will receive " + points + " points.");
//close scanner
keyIn.close();
System.out.println ("Thanks for shopping! See you next time!");
}
}
and it is not working second question
this-System.out.print("Do you have the membership(Y/N) ");
just come out first question and stop
what is my problem?
The program is written correctly except:
Line no 51: else if(totalCost>500) //less than is changed to greater than ( logical error) because the 0.2 discount should be given to greater totalCost ie) greater than 500
Line no 1: //These lines are missing.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Output:
The expected output is achieved.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
double price[] = {26.99, 22.99, 13.99, 56.99, 38.99};
// Variable Declaration
Scanner keyIn = new Scanner(System.in);
//Header
System.out.println( "----------------------------------------\n"
+ " Grocery Shop Price Calculator \n"
+ "----------------------------------------\n") ;
System.out.print ("Please enter the quantities for each item in the list? ");
double totalCost = 0;
double fishAmount = 0;
for(int i=0; i<price.length; i++)
{
int items = Integer.parseInt(keyIn.nextLine());
if(i==4)
fishAmount = price[i]*items;
else
totalCost = totalCost + (price [i]*items);
}
System.out.print("Do you have the membership(Y/N) ");
String st = keyIn.nextLine();
double off;
if(totalCost<250)
off = totalCost*0.1;
else if(totalCost>500) //less than is changed to greater than
off = totalCost*0.2;
else
off = totalCost*0.15;
totalCost = totalCost + fishAmount - off;
long points = 0;
if(st.equalsIgnoreCase("Y"))
{
if(totalCost<500)
points = Math.round(totalCost*2);
else
points = Math.round(totalCost*3);
}
System.out.print("The total price is $"+ totalCost+".");
if(st.equalsIgnoreCase("Y"))
System.out.println("You will receive " + points + " points.");
//close scanner
keyIn.close();
System.out.println ("Thanks for shopping! See you next time!");
}
}