gpt4 book ai didi

java - 循环计算 - Java

转载 作者:行者123 更新时间:2023-12-01 17:27:55 25 4
gpt4 key购买 nike

我无法根据客户购买的瓶子数量来计算他们支付的价格。对于购买的任意数量的瓶子,它都会返回 14 美元的值(value)。请参阅下面的 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

最佳答案

改变||到&&

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

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

关于java - 循环计算 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com