当用户按下两次y并再次按下n时,当他们输入n时,如何显示item1item2的总价

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


这仅将total分配为当前成本。如果要汇总所有费用,则需要使用+=

total += (tax*(price*quantity));

关于java - 用户一次按y两次,下一次按n,当输入n时,如何添加item1和item2并显示总价,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61084139/

10-11 02:36