import java.util.Scanner;
import javax.swing.JOptionPane;
public class RestaurantBill3
{
public static void main(String [] args)
{
//Constant
final double TAX_RATE = 0.0675;
final double TIP_PERCENT = 0.15;
//Variables
double cost;
double taxAmount = TAX_RATE * cost; //Tax amount
double totalWTax = taxAmount + cost; //Total with tax
double tipAmount = TIP_PERCENT * totalWTax; //Tip amount
double totalCost = taxAmount + tipAmount + totalWTax; //Total cost of meal
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the cost of your meal? ");
cost = keyboard.nextDouble();
System.out.println("Your meal cost $" +cost);
System.out.println("Your Tax is $" + taxAmount);
System.out.println("Your Tip is $" + tipAmount);
System.out.println("The total cost of your meal is $" + totalCost);
System.exit(0); //End program
}
}
/*我不断收到错误消息,指出成本显然尚未初始化,但如果它正在等待输入,那么应该如何执行此操作?*/
您在这里指的是初始化之前的cost
值:
double taxAmount = TAX_RATE * cost;
double totalWTax = taxAmount + cost;
将这些变量的初始化移到cost
的初始化之后,这样cost
在被引用时就会有一个值。
我是一名优秀的程序员,十分优秀!