gpt4 book ai didi

java - 餐厅账单,初始化错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:15 26 4
gpt4 key购买 nike

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在被引用时就会有一个值。

关于java - 餐厅账单,初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28442329/

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