gpt4 book ai didi

java - ParadiseInfo2 变量错误代码

转载 作者:行者123 更新时间:2023-12-01 11:19:21 25 4
gpt4 key购买 nike

import java.util.Scanner;
public class ParadiseInfo2
{
public static void main(String[] args)
{
double price;
double discount;
double savings;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >>");
price = keyboard.nextDouble();

System.out.print("Enter discount rates as a whole number >> ");
discount = keyboard.nextDouble();
System.out.println("Special this week on any service over " +
price);
System.out.println("Discount of " + discount + " percent");
System.out.println("That's a savings of at least $" + savings);

displayInfo();
savings = computeDiscountInfo(price, discount);
}
public static void displayInfo()
{
System.out.println("Paradise Day Spa wants to pamper you.");
System.out.println("we will make you look good.");
}
public static double computeDiscountInfo(double pr, double dscnt)
{
double savings;
savings = pr * dscnt / 100;
return savings;

}
}

我在上面的代码中不断收到以下错误。我不知道如何解决它。感谢您的帮助!

ParadiseInfo2.java:18: error: variable savings might not have been initialized System.out.println("That's a savings of at least $" + savings);

最佳答案

main 的最后一行设置了 savings,但您在此之前使用它来print 它。将其更改为类似的内容

// System.out.println("That's a savings of at least $" + savings);
displayInfo();
savings = computeDiscountInfo(price, discount);
System.out.println("That's a savings of at least $" + savings);

关于java - ParadiseInfo2 变量错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31444542/

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