首先,我很肯定这是一个非常简单的答案,我道歉。
我试图弄清楚是否可以使用重载方法通过同一类中的方法链接返回值。在下面的代码中,computeBill(double bookPrice,int quantity)部分是我目前所坚持的部分。
我想要它做的,是通过书的价格和数量。 (main有一个输出部分,将输出传递给这些方法;该部分工作正常)。然后它会将那些传递给moneyConversion(),它执行数学运算,转换为BigDecimal,并将结果作为变量bookPriceDisp返回。我希望computeBill()将相同的值返回给main进行显示。
我在课程结算中找不到符号。我是否需要在每种方法中创建一个新对象?
public class Billing
{
public static BigDecimal computeBill(double bookPrice)
{
BigDecimal bookPriceTaxed = new BigDecimal(bookPriceInput*1.08);
return bookPriceTaxed;
}
public static BigDecimal computeBill(double bookPrice, int quantity)
{
moneyConversion(bookPrice, quantity);
return bookPriceDisp;
}
public static void computeBill(double bookPrice, int quantity, double coupon)
{
System.out.println("Please enter the percentage of your coupon: ");
double couponInput = keyboard.nextDouble();
System.out.println("You entered " + couponInput + "%");
moneyConversion(bookPrice, quantity);
BigDecimal couponSavings = new BigDecimal(bookPrice * quantity * couponInput);
System.out.println("Your books will cost $" + ((bookPriceDisp - couponSavings)*1.08));
System.out.println("Your coupon saved you $" + couponSavings);
}
public static BigDecimal moneyConversion(double bookPrice, int quantity)
{
//Using BigDecimal to get correct dollars/cents.
BigDecimal bookPriceDisp = new BigDecimal(bookPrice * quantity);
bookPriceDisp = bookPriceDisp.setScale(2, BigDecimal.ROUND_HALF_UP);
return bookPriceDisp;
}
谢谢,
-Stephan
答案 0 :(得分:4)
bookPriceDisp
是moneyConversion()
的本地变量。正如其名称所示,局部变量是局部变量,因此在块的外部(即moneyConversion()
方法)不可见/可用。
你想要的是返回moneyConversion()
方法返回的相同值,所以:
// assign the result of moneyConversion() to a local variable result
BigDecimal result = moneyConversion(bookPrice, quantity);
// return this local variable
return result;
或简单地说:
// return the value returned by moneyConversion()
return moneyConversion(bookPrice, quantity);
答案 1 :(得分:1)
我认为问题出在您的方法computeBill(double bookPrice, int quantity)
中。您只需要说return moneyConversion(bookPrice, quantity);
答案 2 :(得分:1)
如果我尝试编译此问题,有几个问题。如前所述,您试图引用超出范围的值。您需要使用方法直接返回的值或使用变量来保存它们,然后在计算中使用该变量,而不是使用这些变量。这是您的程序的编译版本:
import java.math.*;
import java.util.Scanner;
public class Billing
{
static Scanner keyboard = new Scanner(System.in);
public static BigDecimal computeBill(double bookPrice)
{
BigDecimal bookPriceTaxed = new BigDecimal(bookPrice * 1.08);
return bookPriceTaxed;
}
public static BigDecimal computeBill(double bookPrice, int quantity)
{
return moneyConversion(bookPrice, quantity);
}
public static void computeBill(double bookPrice, int quantity, double coupon)
{
System.out.println("Please enter the percentage of your coupon: ");
double couponInput = keyboard.nextDouble();
System.out.println("You entered " + couponInput + "%");
BigDecimal bookPriceDisp = moneyConversion(bookPrice, quantity);
BigDecimal couponSavings = new BigDecimal(bookPrice * quantity * couponInput);
System.out.println("Your books will cost $" + ((bookPriceDisp.subtract(couponSavings)).multiply(new BigDecimal(1.08))));
System.out.println("Your coupon saved you $" + couponSavings);
}
public static BigDecimal moneyConversion(double bookPrice, int quantity)
{
//Using BigDecimal to get correct dollars/cents.
BigDecimal bookPriceDisp = new BigDecimal(bookPrice * quantity);
bookPriceDisp = bookPriceDisp.setScale(2, BigDecimal.ROUND_HALF_UP);
return bookPriceDisp;
}
}
请注意,您不能使用BigDecimal简单地将double值作为常规操作进行多次或减去。您需要使用BigDecimal中可用的方法,例如subtract()和multiply()来进行计算。
希望有所帮助!