java - 简单的java if-else编译错误

标签 java

我正在上计算机编程大学,遇到一个练习题,我一直收到编译器错误:本地变量 movieFee、snackFee 和 couponeFee 未在第 85 行初始化。

我对此很陌生,我觉得我做的一切都是正确的。有人可以帮我吗?

谢谢! 约瑟夫

问题是: 计算去电影院的费用。选择合适的年龄范围—— child 、成人或老年人;选择小吃组合——无、爆米花和汽水、巧克力棒和汽水、或爆米花和糖果和汽水;表明顾客是否有一张优惠券,可以让他在电影费用上享受 2 美元的折扣。

Patron Age Ranges       Movie Fee       Snack Bar Options       Fee
child ( < 12)       $5.50       A - popcorn and pop     $7.90
adult (12 - 65)     $8.50       B - chocolate bar and pop       $6.30
senior ( > 65)      $6.00       C - popcorn and candy and pop       $8.90

我的代码是:

导入java.util.Scanner; 公开课电影费 {

public static void main(String[] args)
{
    //scanner class
    Scanner input=new Scanner(System.in);

    //display the age options and prompt the user to enter one
    System.out.println("Child (<12)");
    System.out.println("Adult (12-65)");
    System.out.println("Senior (>65)");
    System.out.print("Choose your age category: ");
    String age=input.nextLine();

    //display the snak bar options and prompt the user to enter one
    System.out.println("\n\nA - popcorn and pop");
    System.out.println("B - chocolate bar and pop");
    System.out.println("C - popcorn and candy and pop");
    System.out.print("What is your snack bar option(enter a letter): ");
    String snackOption=input.nextLine();

    //calculate the movie fee
    //declare fee variables
    double movieFee, snackFee;
    if(age=="child"||age=="Child")
    {
        movieFee=5.50;
    }
    else if(age=="adult"||age=="Adult")
    {
        movieFee=8.50;
    }
    else if(age=="senior"||age=="Senior")
    {
        movieFee=6.00;
    }
    else
    {
        System.out.println("\nYou did not enter a correct age group.");
        System.out.println("Please try again using one of: child, adult, senior");
    }

    //calculate the snack fee
    if(snackOption=="A"||snackOption=="a")
    {
        snackFee=7.90;
    }
    else if(snackOption=="B"||snackOption=="b")
    {
        snackFee=6.30;
    }
    else if(snackOption=="C"||snackOption=="c")
    {
        snackFee=8.90;
    }
    else
    {
        System.out.println("\nYou did not enter a correct option");
        System.out.println("Please try again using either A, B or C");
    }

    //ask the user if he/she has a coupon
    double couponFee;
    System.out.print("\nDo you have a coupon? Type yes or no: ");
    String coupon=input.nextLine();
    if(coupon=="yes")
    {
        couponFee=2.0;
    }
    else if(coupon=="no")
    {
        couponFee=0;
    }


    //calculate the total fee and total fee
    double result=movieFee+snackFee+couponFee;   //**GETTING ERROR HERE
    System.out.println("Final price is "+result);



}
//end main
}
//end class

最佳答案

您的代码存在两种类型的问题:

第一个问题

您尚未初始化方法局部变量。根据 Java 规范,局部变量需要在使用前初始化。所以做类似的事情:

    double movieFee = 0.0d;
    double snackFee = 0.0d;

第二个问题

字符串应该使用 equals 方法进行比较,而不是使用 == 。所以你必须改变这个和其他条件

    if(age=="child"||age=="Child")

作为

if(age.equals("child") || age.equals("Child"))

关于java - 简单的java if-else编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35307522/

相关文章:

java - 无法使用itext从图像中获取absoluteX和absoluteY

java - 如何用 Java 编写正确的微基准测试?

Java计算器将数字添加到文本字段

java - 在 Gradle Java 项目中使用 Sqlite JDBC 驱动程序

java - 如何隐藏 SWT 组合,使其不占用空间?

map 的 Java 方法引用 - IntelliJ 警告

java - 将 Java 泛型转换为 Kotlin 泛型

java - 如何在weka中将测试文档转换为训练词汇维度的 vector ?

java - 如何在用户输入后立即打印 JTextField 值?

java - Hibernate:一对多getId()不获取竞争对象