gpt4 book ai didi

java - 需要有关嵌套 if 的建议

转载 作者:行者123 更新时间:2023-12-01 10:59:29 25 4
gpt4 key购买 nike

我尝试在下面的代码中使用嵌套的 if 。我已经初始化了变量,但编译器告诉我名为“bill”的变量尚未初始化,即使它已初始化。为什么编译器无法识别分配给变量的值?请参阅下面代码中的注释。

package killMe;

import java.util.Scanner;

public class Kill_Me {

static Scanner console = new Scanner(System.in);

static double PREMIUM_SERVICE = 55.00;
static double PREMIUM_DAY_OVERTIME_MIN = 0.20;
static double PREMIUM_NIGHT_OVERTIME_MIN = 0.15;
static double REGULAR_SERVICE = 30.00;
static double REGULAR_OVERTIME_MIN = 0.40;

public static void main(String[] args) {
int acctNumber;
double premiumDayMin;
double premiumNightMin;
double bill;
double minutes;
String name;
String premium = "PREMIUM";
String regular = "REGULAR";

System.out.println("What is the Account Number? ");
acctNumber = console.nextInt();
System.out.println("What is the Customer Name? ");
name = console.next();
System.out.println("Is the Service Code Premium or Regular? ");
String strService = console.next();
String strServiceCAP = strService.toUpperCase();


if(strServiceCAP.compareTo(premium) == 0)
{
System.out.println("How many Day Minutes were used? ");
premiumDayMin = console.nextDouble();
System.out.println("How many Night Minutes were used? ");
premiumNightMin = console.nextDouble();


if(premiumDayMin <0 && premiumNightMin <0)
{
System.out.println("Minutes cannot be less than 0 ");
}
else if(premiumDayMin <= 75 && premiumNightMin <= 100)
{
bill = PREMIUM_SERVICE;
}
else bill = PREMIUM_SERVICE + (premiumDayMin - 75) * PREMIUM_DAY_OVERTIME_MIN + (premiumNightMin - 100)
* PREMIUM_NIGHT_OVERTIME_MIN;

minutes = premiumDayMin + premiumNightMin;

System.out.println("Customer Name: " + name);
System.out.println("Account Number: " + acctNumber);
System.out.println("Service Type: " + strServiceCAP);
System.out.println("Minutes Premium Service Used (Day): " + premiumDayMin);
System.out.println("Minutes Premium Service Used (Night): " + premiumNightMin);
System.out.println("Amount Due: " + bill); // I get an error here stating, "The local variable 'bill' may not have been initialized".
}
else if(strServiceCAP.compareTo(regular) == 0)
{
System.out.println("How many minutes were used? ");
minutes = console.nextDouble();
bill = REGULAR_SERVICE + (minutes - 50) * REGULAR_OVERTIME_MIN;

System.out.println("Customer Name: " + name);
System.out.println("Account Number: " + acctNumber);
System.out.println("Service Type: " + strServiceCAP);
System.out.println("Minutes Regular Service Used: " + minutes);
System.out.println("Amount Due: " + bill); // I DO NOT receive an error message here.


}
else
{
System.out.println("Invalid Service Type");
}


} // End of main

} // End of class

最佳答案

否,bill 在所有情况下均未初始化。

理解这一点:Java 编译器永远不会计算 boolean 表达式;简化版:

double bill;

if (c1) {
bill = v1;
} else if (c2) {
bill = v2;
}

// try and use bill here

即使,根据您的逻辑, boolean 表达式 c1c2 可能涵盖所有可能的情况,编译器也无法确保这是案例。

无论 if/else、switch 等语句嵌套得有多深,这都是错误的根本原因。

关于java - 需要有关嵌套 if 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446814/

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