简单java if-else编译错误
我正在上大学学习计算机编程,遇到了一个实践问题,我经常遇到一个编译器错误:本地varialbe movieFee、snackFee和couponeFee未在第85行初始化 我是新手,我觉得我做的每件事都是对的。谁能帮我一下吗 谢谢大家!! 约瑟夫 问题是: 计算去电影院的费用。选择适当的年龄范围-儿童、成人或老年人;选择快餐店套餐-无,爆米花和汽水,巧克力和汽水,或爆米花和糖果和汽水;说明顾客是否有一张优惠券,可以从电影费中扣除2美元简单java if-else编译错误,java,Java,我正在上大学学习计算机编程,遇到了一个实践问题,我经常遇到一个编译器错误:本地varialbe movieFee、snackFee和couponeFee未在第85行初始化 我是新手,我觉得我做的每件事都是对的。谁能帮我一下吗 谢谢大家!! 约瑟夫 问题是: 计算去电影院的费用。选择适当的年龄范围-儿童、成人或老年人;选择快餐店套餐-无,爆米花和汽水,巧克力和汽水,或爆米花和糖果和汽水;说明顾客是否有一张优惠券,可以从电影费中扣除2美元 Patron Age Ranges Movie
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
惠顾范围电影费快餐店选择费
儿童(<12)$5.50一个-爆米花和爆米花$7.90
成人(12-65)$8.50 B-巧克力和汽水$6.30
高级(>65)$6.00 C-爆米花、糖果和汽水$8.90
我的代码是:
导入java.util.Scanner;
公费
{
publicstaticvoidmain(字符串[]args)
{
//扫描器类
扫描仪输入=新扫描仪(System.in);
//显示年龄选项并提示用户输入一个
System.out.println(“儿童(65)”;
System.out.print(“选择您的年龄类别:”);
字符串年龄=input.nextLine();
//显示障碍栏选项并提示用户输入一个
System.out.println(“\n\nA-爆米花和爆米花”);
System.out.println(“B-巧克力和汽水”);
System.out.println(“C-爆米花、糖果和汽水”);
System.out.print(“您的快餐店选项是什么(输入字母):”;
String snackOption=input.nextLine();
//计算电影费用
//声明费用变量
双倍电影费,小吃费;
如果(年龄=“儿童”| |年龄=“儿童”)
{
电影费=5.50;
}
否则如果(年龄=“成人”| |年龄=“成人”)
{
电影费=8.50;
}
否则如果(年龄=“高级”| |年龄=“高级”)
{
电影费=6.00;
}
其他的
{
System.out.println(“\n您没有输入正确的年龄组。”);
System.out.println(“请使用以下选项之一重试:儿童、成人、老年人”);
}
//计算快餐费
如果(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;
}
其他的
{
System.out.println(“\n您没有输入正确的选项”);
System.out.println(“请使用A、B或C重试”);
}
//询问用户是否有优惠券
双倍耦合费;
System.out.print(“\n您有优惠券吗?键入是或否:”);
字符串优惠券=input.nextLine();
如果(优惠券=“是”)
{
couponFee=2.0;
}
否则如果(优惠券=“否”)
{
couponFee=0;
}
//计算总费用和总费用
双重结果=电影费+零食费+耦合费;//**此处获取错误
System.out.println(“最终价格为”+结果);
}
//端干管
}
//末级
在某些情况下,由于ifs条件,这些变量不会在编译器报告的行初始化。在某些情况下,由于ifs条件,这些变量不会在编译器报告的行初始化。使用.equals()比较字符串
例如:
此外,还可以使用equalsIgnoreCase()
例如:
最后,不是:
else if(coupon=="no") {
couponFee=0;
}
写:
else {
couponFee = 0.0;
}
另一个问题:您没有在while循环中检查年龄,因此如果输入了无效的年龄,您只需通知用户,但不再询问。使用.equals()比较字符串 例如: 此外,还可以使用equalsIgnoreCase() 例如: 最后,不是:
else if(coupon=="no") {
couponFee=0;
}
写:
else {
couponFee = 0.0;
}
另一个问题:您没有在while循环中检查年龄,因此如果输入了无效的年龄,您只需通知用户,但不再询问。您的代码有两种类型的问题: 第一个问题 您尚未初始化方法局部变量。根据Java规范,局部变量需要在使用前进行初始化。所以你可以这样做:
double movieFee = 0.0d;
double snackFee = 0.0d;
第二个问题
字符串应使用equals方法进行比较,而不是使用==。所以你必须改变这个条件和其他条件
if(age=="child"||age=="Child")
作为
代码存在两种类型的问题: 第一个问题 您尚未初始化方法局部变量。根据Java规范,局部变量需要在使用前进行初始化。所以你可以这样做:
double movieFee = 0.0d;
double snackFee = 0.0d;
第二个问题
字符串应使用equals方法进行比较,而不是使用==。所以你必须改变这个条件和其他条件
if(age=="child"||age=="Child")
作为
在下面的
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;
}
这意味着您必须通过以下方式帮助编译器
couponFee=0提供默认值代码>
couponFee
变量设置一个默认值,并使代码简短
double couponFee = 0;
System.out.print("\nDo you have a coupon? Type yes or no: ");
if("yes".equals(input.nextLine()) couponFee=2.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;
}
这意味着您必须通过以下方式帮助编译器
couponFee=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
import java.util.Scanner;
public class MovieFee {
public static void main(String[] args)
{
//scanner class
Scanner input=new Scanner(System.in);
//declare fee variables
double movieFee = 0.0;
double snackFee = 0.0;
double couponFee = 0.0;
do {
//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();
//calculate the movie fee
if(age.equalsIgnoreCase("child"))
{
movieFee=5.50;
}
else if(age.equalsIgnoreCase("adult"))
{
movieFee=8.50;
}
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\n");
}
} while(movieFee==0.0);
do {
//display the snak bar options and prompt the user to enter one
System.out.println("\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 snack fee
if(snackOption.equalsIgnoreCase("a"))
{
snackFee=7.90;
}
else if(snackOption.equalsIgnoreCase("b"))
{
snackFee=6.30;
}
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");
}
} while (snackFee==0.0);
String coupon="";
do {
//ask the user if he/she has a coupon
System.out.print("\nDo you have a coupon? Type yes or no: ");
coupon=input.nextLine();
if(coupon.equalsIgnoreCase("yes"))
{
couponFee=2.0;
}
else if(coupon.equalsIgnoreCase("no"))
{
couponFee=0;
}
} while (!coupon.equalsIgnoreCase("yes") && !coupon.equalsIgnoreCase("no"));
//calculate the total fee and total fee
double result = movieFee + snackFee + couponFee;
System.out.println("Final price is "+result);
}
//end main
}
//end class