I can’t figure out how to return the value of finalCost to the main method and have it printed.
import java.util.*; public class PhonePlan { private static double basecost = 8.5; private static double rate = 0.35; private static double discount = 0.15; public static void main(String[] args) { //Scan input for downloadLimit Scanner scan = new Scanner(System.in); System.out.print("Enter the download limit in GB: "); int downloadLimit = scan.nextInt(); // Call other method calcMonthlyCharge(downloadLimit); System.out.println("Plan monthly cost: " + calcMonthlyCharge(finalCost)); } public static double calcMonthlyCharge(double downloadLimit) { // Calculate final cost double fullCost = downloadLimit * rate + basecost; double planDiscount = fullCost * discount; double finalCost = fullCost - planDiscount; return finalCost; } }
Specifically, I can’t find how to use the returned value in the “println” line.
System.out.println("Plan monthly cost: " + calcMonthlyCharge(finalCost) ); } public static double calcMonthlyCharge(double downloadLimit) { // Calculate final cost double fullCost = downloadLimit * rate + basecost; double planDiscount = fullCost * discount; double finalCost = fullCost - planDiscount; return finalCost; }
Advertisement
Answer
You call calcMonthlyCharge(downloadLimit)
,but don’t store the returnvalue.
When you call System.out.println("Plan monthly cost: " + calcMonthlyCharge(finalCost) );
It is unknown what finalcost is, this is a variable which only exist in the scope of calcMonthlyCharge
Either store the returnvalue of calcMonthlyCharge(downloadLimit)
and reuse the value to print, or use calcMonthlyCharge(downloadLimit)
in your println
with downloadLimit as parameter to get a new returnvalue.