Java POS程序。折扣全部,而不是每个价格

时间:2017-09-17 04:32:59

标签: java

我正在上课。我试图从总价中打折“CA Resident 5%”,但我认为我的代码单独打折每个价格然后添加它们。不确定我的程序在哪里发生这种情况。

我在基础级java,第二周,所以我不想学习任何太高级的东西,但要了解我的基本代码做错了什么。感谢

import java.util.*;

public class Sept13 {

   public static void main(String[] args) {

/*
========Point of Sale========
Foods           No Tax
Ciggarettes     25% Tax
Books           No Tax
Computer        10% Tax
CA Resident     5% Discount
Clothes         10 % Tax

*/




boolean CAR = false;
double total = 0, price = 0, Ca = .05, Fo, Ci = .25, Bo, Co = .10, Cl = .10;
int ans = 0;

Scanner in = new Scanner(System.in); 

System.out.print("Are you a California Resident?  Type 1 if yes type 0 if no ");
		ans = in.nextInt();
      
      if (ans == 1){
         CAR = true; //Discount for California Resident
         }
         
 
//Cigarette Tax
System.out.print("Are you buying cigarettes?  Type 1 if yes type 0 if no ");
		ans = in.nextInt();
      

      
      if (ans == 1);
         System.out.print("Enter price of item(s)");
		   price = in.nextDouble();
         
      if (CAR){
         total += ((price*Ci)+price);
         }
         
System.out.print(total);


//Computer Tax
System.out.print("Are you buying a computer(s)?  Type 1 if yes type 0 if no ");
		ans = in.nextInt();
      
      
      if (ans == 1);
         System.out.print("Enter price of item(s)");
		   price = in.nextDouble();
         
      if (CAR){
         total += ((price*Co)+price);
         }
System.out.print(total);


//Clothes Tax   
System.out.print("Are you buying a Clothes?  Type 1 if yes type 0 if no ");
		ans = in.nextInt();
      
      
      if (ans == 1);
         System.out.print("Enter price of item(s)");
		   price = in.nextDouble();
         
      if (CAR){
         total += ((price*Cl)+price);
         }
         
         

System.out.print (total-(total*Ca));


      
   

   }
}

1 个答案:

答案 0 :(得分:0)

试试这个,我认为这是解决您问题的方法。

//Cigarette Tax
System.out.print("Are you buying cigarettes?  Type 1 if yes type 0 if no ");

ans = in.nextInt();

if (ans == 1){
   double cigarateTax = 0;
   System.out.print("Enter price of item(s)");

   if (CAR){
         cigarateTax  += ((price*Ci)+price);
   }
   total += cigarateTax;
}
System.out.print(total);