2017-09-17 39 views
0

我正在從課堂上工作。我試圖從總價中折扣「CA居民5%」,但我認爲我的代碼是單獨折扣每個價格然後添加它們。不知道在我的程序中發生了什麼。Java POS程序。折扣全部,不是每個價格

我在基礎級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 "); 
 
\t \t 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 "); 
 
\t \t ans = in.nextInt(); 
 
     
 

 
     
 
     if (ans == 1); 
 
     System.out.print("Enter price of item(s)"); 
 
\t \t 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 "); 
 
\t \t ans = in.nextInt(); 
 
     
 
     
 
     if (ans == 1); 
 
     System.out.print("Enter price of item(s)"); 
 
\t \t 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 "); 
 
\t \t ans = in.nextInt(); 
 
     
 
     
 
     if (ans == 1); 
 
     System.out.print("Enter price of item(s)"); 
 
\t \t price = in.nextDouble(); 
 
      
 
     if (CAR){ 
 
     total += ((price*Cl)+price); 
 
     } 
 
      
 
      
 

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

 

 
     
 
    
 

 
    } 
 
}

+1

那麼,你是每個項目後單獨打折每個價格輸入之前,將它們添加到總數。如果您希望折扣在最後發生,只需在所有輸入處於輸入狀態並應用折扣後編寫一個'if'語句。 – alejandrogiron

+0

另外,我非常肯定,單獨應用折扣或在最後應用折扣會輸出相同的結果。 – alejandrogiron

回答

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