once the user presses 2 times y
and the next time n
when they enter n
how to display the total price of item1
and item2
import java.util.Scanner;
import javax.swing.JOptionPane;
public class ITPL_Coursework {
public static void main(String[] args) {
String itemStr ;
String quantityStr;
final double tax=0.03;
double total = 0;
int price;
char choice;
// System.out.print("\nEnter number of the hardware component you want: ");
System.out.println("\tHari's Hardware Company ");
System.out.println("\t-----------------------");
System.out.println("\nAvailable hardware components and its price are listed below: \n1:HYPERX FURY RAM \n2:Graphics Card");
do{
itemStr=JOptionPane.showInputDialog("Enter number of the hardware component you want: ");
int item=Integer.parseInt(itemStr);
quantityStr=JOptionPane.showInputDialog("Enter the quanity of the hardware component you want:");
int quantity=Integer.parseInt(quantityStr);
if(item==1){
price=1500;
total=(tax*(price*quantity));
System.out.println("You choose to buy HYPERX FURY RAM for: " +total);
}
if(item==2){
price=1000;
total=(tax*(price*quantity));
System.out.println("You choose to buy Graphics Card for: " +total);
}
System.out.print("Do you want to continue y/n?:");
Scanner console = new Scanner(System.in);
choice=console.next().charAt(0);
}
while(choice=='y');
System.out.println(""+total);
}
}
total=(tax*(price*quantity));
This only assigns total
to be the current cost. If you want to sum all costs, then you need to use +=
:
total += (tax*(price*quantity));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.