2012-11-26 201 views
0

我無法根據購買的瓶子數量計算出客戶支付的價格。對於購買的任何數量的瓶子,它總是返回14美元的價值。請參閱下面的Java代碼:循環計算 - Java

// PriceCalculator class that calculates the prices for customers 
// counter controlled repetition 

import java.util.Scanner; // program uses class Scanner 

public class PriceCalculator 
{ 
private String beginSale; // name of shop this price is calculated 

// constructor initializes beginSale 
public PriceCalculator(String name) 
{ 
    beginSale = name; // initializes shopName 
} // end constructor 

// method to set the shop name 
public void setBeginSale (String name) 
{ 
    beginSale = name; // set the welcome address 
} // end method setBeginSale 

// method to retrieve the welcome address 
public String getBeginSale() 
{ 
    return beginSale; 
} // end method getBeginSale 

// display a welcome message to the shopper 
public void displayMessage() 
{ 
    // getBeginSale gets the thank you address 
    System.out.printf("Welcome to Price Calculator for Purchasing Wines at %s!\n", 
     getBeginSale()); 
} // end method displayMessage 

// determine charges based on 5 customers who shopped 
public void calculatePrice() 
{ 
    // create scanner to obtain input from command window 
    Scanner input = new Scanner(System.in); 

    int basePrice; // base price of a bottle of wine 
    String customer; // name of customer 
    int gradeCounter; // number of customers to be entered next 
    int bottles; // number of bottles purchased 
    double discount; // discount value on the number of bottles 
    double rate; 
    double total; // total costs of wine purchased 

    // initialization phase 
    basePrice = 10; // initialize base price 
    gradeCounter = 1; // initialize loop counter 
    bottles = 1; // initialize bottles 
    discount = (basePrice * bottles); // initialize discount 
    rate = 0; 
    customer = null; // initialize customer name 

    // processing phase uses counter-controlled repetition 
    while (gradeCounter <= 3) // loop 5 times 
    { 
     System.out.print("Enter the name for customer " + gradeCounter + " : "); // prompt 
     customer = input.next(); // input next name 
     System.out.print("Enter the number of bottles for customer " + gradeCounter + " : "); // prompt 
     bottles = input.nextInt(); // input next bottles sold 
     gradeCounter = gradeCounter + 1; // increment counter by 1 
     discount = (basePrice * bottles) - (rate * bottles); 

     System.out.printf("\nThe Price for Customer - %s is : $%.2f\n\n", 
      customer, discount); 
    } // end while loop 

    // calculate purchase based on number of bottles sold 

    if (bottles <= 6) 
    { 
     discount = (basePrice * bottles) - (rate * bottles); // calculates discount 
    } 
    else if 
    (bottles > 6 && bottles<= 12) 
     { 
     discount = (basePrice * bottles) - (rate * bottles); // calculates discount 
     } 
    else if (bottles > 48) 
     System.out.println ("It is forbidden to sell above the maximum range"); 

} // end method calculatePrice 
} // end class PriceCalculator 
+0

我想,你caluclate價格之前,你不應該結束while循環。請嘗試在while循環本身中計算價格。如果(瓶子<= 6) 折扣=(basePrice *瓶子);並且使用 如果(瓶子<= 6) 折扣= //計算折扣 } 否則如果(瓶子> 6 &&瓶子<= 12) 折扣*折扣* 0.05 *瓶子* basePrice; (瓶> 48) System.out.println(「禁止超出最大範圍銷售」); –

回答

1

很簡單;您將在輸入數據的循環中打印價格,以便價格不會發生變化,並且一次又一次地打印相同的語句。

只需推動以下語句:

System.out.printf("\nThe Price for Customer - %s is : $%.2f\n", customer, discount); 
} // end while loop 

後這一行:

System.out.println("It is forbidden to sell above the maximum range"); 

輸出將是:

Welcome to Price Calculator for Purchasing Wines at Red Rocky! 
Enter the name for customer 1: asdfsa 
Enter the number of bottles for customer 1: 3 

The Price for Customer - asdfsa is : $42.00 
Enter the name for customer 1: gasdfs 
Enter the number of bottles for customer 1: 4 

The Price for Customer - gasdfs is : $56.00 
Enter the name for customer 1: asfasdf 
Enter the number of bottles for customer 1: 5 

The Price for Customer - asfasdf is : $70.00 

你也可以更換該聲明

System.out.print("Enter the name for customer 1: "); 

System.out.print("Enter the name for customer " + gradeCounter + ":"); 

最終代碼看起來像:

while (gradeCounter <= 3) // loop 3 times 
{ 
    System.out.print("Enter the name for customer 1: "); // prompt 
    customer = input.next(); // input next name 
    System.out.print("Enter the number of bottles for customer 1: "); // prompt 
    bottles = input.nextInt(); // input next bottles sold 
    gradeCounter = gradeCounter + 1; // increment counter by 1 
// calculate purchase based on number of bottles sold 
    if (bottles <= 6) { 
     discount = (basePrice * bottles); // calculates discount 
    } else if (bottles > 6 || bottles <= 12) { 
     discount = discount * 0.05 * bottles * basePrice; // calculates discount 
    } else if (bottles > 48) 
    System.out.println("It is forbidden to sell above the maximum range"); 
    System.out.printf("\nThe Price for Customer - %s is : $%.2f\n",customer, discount); 
} // end while loop 
+0

感謝您的肯定答覆。唯一剩下的就是如果購買的瓶子數量超過6個或等於12個,我應該對價格應用5%(0.05)的折扣。請問我該如何繼續? – akinboj

+0

不需要做任何事......查看更新的答案。 – SiB

+0

@SiB - 需要改變,如果要GT 6 ** AND ** LTEQ 12或它捕捉所有 – mcalex

1

你想改變你的

if (bottles > 6 || bottles <= 12) 

if (bottles > 6 && bottles <= 12) 
1

您沒有更改任何值...首先,你設置折扣值:

discount = (basePrice * bottles); // initialize discount 

然後在while lo op,它從不根據給定的值更新:

System.out.printf("\nThe Price for Customer - %s is : $%.2f\n", 
     customer, discount); 

不應該在printf之前的while循環內設置折扣值嗎? :)

或者更明確:

// processing phase uses counter-controlled repetition 
while (gradeCounter <= 3) // loop 3 times 
{ 
    System.out.print("Enter the name for customer 1: "); // prompt 
    customer = input.next(); // input next name 
    System.out.print("Enter the number of bottles for customer 1: "); // prompt 
    bottles = input.nextInt(); // input next bottles sold 
    gradeCounter = gradeCounter + 1; // increment counter by 1 
discount = (basePrice * bottles); 
    System.out.printf("\nThe Price for Customer - %s is : $%.2f\n", 
     customer, discount); 
} // end while loop 
2

變化||到& &

if (bottles > 6 || bottles <= 12) // Your code 

if (bottles > 6 && bottles <= 12) // changes