Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一旦用户按下2次y和下一次n,当输入n时,如何添加项目1和项目2并显示总价_Java - Fatal编程技术网

Java 一旦用户按下2次y和下一次n,当输入n时,如何添加项目1和项目2并显示总价

Java 一旦用户按下2次y和下一次n,当输入n时,如何添加项目1和项目2并显示总价,java,Java,一旦用户按下2次y和下一次n时,他们输入n如何显示item1和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

一旦用户按下2次
y
和下一次
n
时,他们输入
n
如何显示
item1
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));

伙计们,代码运行良好,我的讲师说不需要使用gui。当我选择第1项和第2项时。。然后按n键,我想显示item1和Item2的相加。现在,当我选择item1和Item2时,它只显示Item2的价格。请显示您的程序的功能,并解释您希望它以不同方式执行的功能。显示一个特定的示例来说明一般描述。旁注:我建议您了解Java缩进标准。你的缩进到处都是,这会分散你的注意力,使你无法阅读代码来了解它的功能。输出图像------所以最后我只显示System.out.println(+total)??兄弟,我试过了,但它不起作用,仍然显示相同的输出,看我上传的照片,仍然显示物品2的价格,我解决了,非常感谢你,忠诚
total += (tax*(price*quantity));