once user press 2 times y and the next time n, when enter n how to add item1 and item2 and display total price

huangapple go评论76阅读模式
英文:

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 user press 2 times y and the next time n, when enter n how to add item1 and item2 and display total price

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));

huangapple
  • 本文由 发表于 2020年4月7日 23:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61084139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定