简单的java if-else编译错误

时间:2016-02-10 05:28:46

标签: java

我正在上大学进行计算机编程并遇到了一个练习题,我不断收到编译错误:本地varialbe movieFee,snackFee和couponeFee未在第85行初始化。

我是新手,我觉得我做得很对。有人可以帮助我吗?

谢谢! 约瑟夫

问题是: 计算去电影院的费用。选择适当的年龄范围 - 儿童,成人或老年人;选择小吃店组合 - 没有,爆米花和流行音乐,巧克力棒和流行音乐,或爆米花和糖果和流行音乐;表明赞助人是否有优惠券可以让他减去2美元的电影费。

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

我的代码是:

import java.util.Scanner; 公共课MovieFee {

var controltypeid = [];
$('#firstDiv > a[controltypeid]').each(function (i, v) {
 controltypeid.push($(v).attr( "controltypeid" ));
});
console.log(controltypeid);

8 个答案:

答案 0 :(得分:2)

您的代码有两类问题:

第一个问题

您尚未初始化方法局部变量。根据Java规范,本地变量需要在使用前初始化。所以做一些像:

Map

第二个问题

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

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

作为

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

答案 1 :(得分:0)

在某些情况下,由于ifs条件,这些变量不会在编译器报告的行中初始化。

答案 2 :(得分:0)

使用.equals()比较字符串。

E.g:

if(snackOption.equals("A") || snackOption.equals("a")) {
    snackFee = 7.90;
}

此外,您可以使用equalsIgnoreCase()。

E.g:

if(snackOption.equalsIgnoreCase("A")) {
    snackFee = 7.90;
}

最后,而不是:

else if(coupon=="no") {
    couponFee=0;
}

写:

else {
    couponFee = 0.0;
}

另一个问题:你没有在一个循环中检查年龄,所以如果输入了无效年龄,你只需通知用户,但你不要再问。

答案 3 :(得分:0)

在以下if else-if条件下,您的条件没有默认捕手。

编译器在编译时不能假设 couponFee变量在运行时具有默认值,或者它将在if或else-if中命中。

    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;
    }

这意味着你必须通过

来帮助编译器
  1. 提供couponFee = 0;
  2. 的默认值
  3. 添加else条件,这将有助于编译器知道变量不会保持未初始化状态;
  4. 但如果我是你,我只会为couponFee变量设置默认值并使代码变短。

        double couponFee = 0;
        System.out.print("\nDo you have a coupon? Type yes or no: ");
        if("yes".equals(input.nextLine()) couponFee=2.0;
    

答案 4 :(得分:0)

import java.util.Scanner;

public class MovieFee {

    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 = 0.0, snackFee = 0.0;
        //if (age == "child" || age == "Child") {
        if (age.equalsIgnoreCase("child")) {
            movieFee = 5.50;
        }
        //else if (age == "adult" || age == "Adult") {
        else if (age.equalsIgnoreCase("adult")) {
            movieFee = 8.50;
        }
        //else if (age == "senior" || age == "Senior") {
        else if (age.equalsIgnoreCase("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") {
        if (snackOption.equalsIgnoreCase("a")) {
            snackFee = 7.90;
        }
        //else if (snackOption == "B" || snackOption == "b") {
        else if (snackOption.equalsIgnoreCase("b")) {
            snackFee = 6.30;
        }
        //else if (snackOption == "C" || snackOption == "c") {
        else if (snackOption.equalsIgnoreCase("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");
            // You need to send them back to the start, or exit the program.
        }

        // ask the user if he/she has a coupon
        double couponFee = 0.0;
        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

答案 5 :(得分:0)

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

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


    //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: ");
    age=input.nextLine();
    System.out.println("============" +age);

    //calculate the movie fee
    //declare fee variables

    double movieFee=0, snackFee=0;
    if(age.equals("child")||age.equals("Child"))
    {
        movieFee=5.50;
        movieFee=movieFee-couponFee;
        System.out.println("your movie Fee is" +movieFee);
    }
    else if(age.equals("adult")||age.equals("Adult"))
    {
        movieFee=8.50;
        movieFee=movieFee-couponFee;
        System.out.println("your movie Fee is" +movieFee);
    }
    else if(age.equals("senior")||age.equals("Senior"))
    {
        movieFee=6.00;
        movieFee=movieFee-couponFee;
        System.out.println("your movie Fee is" +movieFee);
    }
    else
    {
        System.out.println("\nYou did not enter a correct age group.");
        System.out.println("Please try again using one of: child, adult, senior");
    }

    //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): ");
    snackOption=input.nextLine();

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



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



}
//end main

答案 6 :(得分:0)

  1. 初始化变量或在没有条件的情况下为其赋予值。

  2. 使用Private Sub CommandButton1_Click() Dim WB1 As Workbook Dim WB2 As Workbook Set WB1 = ActiveWorkbook Set WB2 = Workbooks.Open("C:\Users\joseph\Desktop\Required Files\Almost final\ RawData.xlsm") WB1.Sheets("CR Details").Columns("A:AW").Value = WB2.Sheets("sheet1").Columns("A:AW").Value WB2.Close End Sub 比较字符串。

  3. 显示选项并立即进行验证。

    equalsIgnoreCase()

答案 7 :(得分:-1)

关于未在第一个定义和if-else块情况下初始化的变量,您的变量可能尚未初始化。所以你得到了错误。   您可以在第一次定义时初始化所有变量,   或者你应该在&#34; else&#34;上初始化变量;块(不仅仅是其他)。

Java编译时错误让你想到!认为客户端在运行时犯错误的可能性。像你这样的东西可能会进入&#39;优惠券而不是是 - 否。

此致