英文:
once user press 2 times y and the next time n, when enter n how to add item1 and item2 and display total price
问题
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.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);
}
}
英文:
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);
}
}
答案1
得分: 1
total=(tax*(price*quantity));
这只是将 total
赋值为当前成本。如果你想要累加所有成本,那么你需要使用 +=
:
total += (tax*(price*quantity));
英文:
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论