[英]Dr Java error on code
It is saying that my local variable newaccbalance
may not have been initialized. 就是说我的局部变量newaccbalance
可能尚未初始化。 I know I declared it as a double. 我知道我宣布为双重。 Help please 请帮助
import java.util.*;
public class Pg244Problem12 {
public static void main(String[] args)
{
int accnum, minbalance, currentbalance;
int acctype;
double newaccbalance;
Scanner console = new Scanner(System.in);
System.out.println("Enter the customer's account number:");
accnum = console.nextInt();
System.out.println("Enter the customer's account type by using the number 1 for Checking or 2 for Savings:");
acctype = console.nextInt();
System.out.println("Enter the minimum balance the customer's account can have:");
minbalance = console.nextInt();
System.out.println("Enter the current balance of the customer's account:");
currentbalance = console.nextInt();
// Checkings
if(acctype == 1 && currentbalance >= (minbalance+5000)){
newaccbalance = ((currentbalance*.05)*(1/12));
}
if (acctype == 1 && currentbalance >= minbalance && currentbalance < (minbalance+5000)){
newaccbalance = ((currentbalance*.03)*(1/12));
}
if (acctype == 1 && currentbalance < minbalance){
newaccbalance = (currentbalance-25);
}
// Savings
if (acctype == 2 && currentbalance >= minbalance){
newaccbalance = ((currentbalance*.04)*(1/12));
}
if (acctype == 2 && currentbalance < minbalance){
newaccbalance = (currentbalance - 10);
}
System.out.println("The account number is: "+ accnum);
System.out.println("The account type is: "+ acctype);
System.out.println("The current balance is: "+ currentbalance);
System.out.println("The new account balance is: "+ newaccbalance);
}
}
First of all, declaring and initializing is not the same thing. 首先,声明和初始化不是一回事。
double newaccbalance;
declares the variable. 声明变量。
newaccbalance = 42;
is initializing the variable. 正在初始化变量。
The problem in your code is that the compiler can not guarantee that any of your if-statements will be true, therefore it is possible for newaccbalance
to be left uninitialized. 您的代码中的问题是,编译器无法保证您的任何if语句都为true,因此newaccbalance
可能未初始化。
I suggest two things: 我建议两件事:
First of all, initialize the variable to a default value, double newaccbalance = 0;
首先,将变量初始化为默认值, double newaccbalance = 0;
will both declare and initialize the variable. 将同时声明和初始化变量。
Secondly, change the structure of your if-statements and also use if-else-if, something like this: 其次,更改if语句的结构,并使用if-else-if,如下所示:
if (acctype == 1) {
// For these if statements, acctype is 1 so we don't need to check that again
if(currentbalance >= (minbalance+5000)){
newaccbalance = ((currentbalance*.05)*(1/12));
}
else if (currentbalance >= minbalance) {
// && currentbalance < (minbalance+5000) will be true because the above if-statement is **not** true
newaccbalance = ((currentbalance*.03)*(1/12));
}
else {
// if (acctype == 1 && currentbalance < minbalance) would always be true here
newaccbalance = (currentbalance-25);
}
}
else if (acctype == 2){
// Savings
if (currentbalance >= minbalance) {
newaccbalance = ((currentbalance*.04)*(1/12));
}
else { // currentbalance < minbalance) is always true here
newaccbalance = (currentbalance - 10);
}
}
else {
// acctype is neither 1 or 2, what should we do now? RuntimeError, Catastrophic failure, the monsters are coming! We're screwed!
}
You are declaring your variable. 您正在声明变量。 You need to initialize your variable. 您需要初始化变量。
Declaring is where you create the variable: 声明是创建变量的地方:
double newaccbalance;
Initializing is where you assign a variable a value: 初始化是为变量分配值的地方:
newaccbalance = 0;
So what you need to do is: 因此,您需要做的是:
double newaccbalance = 0.0;
All your assignments are wrapped in if
control structures. 您的所有分配都包装在if
控制结构中。 If none of the conditions is evaluated to true
, the variable will remain unassigned. 如果没有条件被评估为true
,则变量将保持未分配状态。 As a local
variable it does not get a default value either. 作为local
变量,它也不会获得默认值。
That is why the message says that it may be not initialized. 这就是为什么该消息说它可能没有初始化的原因。
It is not initialized. 它没有初始化。 When you declare it try double newaccbalance = 0.0; 当您声明它时,请尝试使用double newaccbalance = 0.0;
I think the problem is newaccbalance is only set conditionally (in the if statements) so it can never be guaranteed to be set to a value. 我认为问题在于newaccbalance仅是有条件地设置(在if语句中),因此永远不能保证将其设置为一个值。
Initializing and declaring are two different things. 初始化和声明是两件事。
I don't think you're getting an error, but a warning. 我不认为您会收到错误,而是警告。
Regardless, Java is correct. 无论如何,Java是正确的。
Your variable newaccbalance may not have been initialized. 您的变量newaccbalance可能尚未初始化。
You've declared it a double, but you only assign it a value inside if statements. 您已将其声明为double,但仅在if语句内为其分配了一个值。
Java does not know whether those if statements cover all possible cases and therefore warns you what newaccbalance
may in fact be unassigned. Java不知道这些if语句是否涵盖所有可能的情况,因此会警告您实际上可能未分配什么newaccbalance
。
Note that Java does not assign a zero value to undefined variables. 请注意,Java不会为未定义的变量分配零值。
You have to do that yourself. 你必须自己做。
Change the top declaration to : 将顶部声明更改为:
double newaccbalance = 0; //Or whatever default value you want.
Either that or add an extra else
behind the last one like so: 要么在最后一个后面添加else
,如下所示:
else if (acctype == 2 && currentbalance < minbalance){
newaccbalance = (currentbalance - 10);
}
else { newaccbalance = 0; }
This will ensure to the compilers satisfaction the newaccbalance has a defined value, rather than a random one. 这将确保编译器满意,即newaccbalance具有定义的值,而不是随机的值。
You should always ensure that this is the case and kuddo's for heeding the warning and taking action. 您应该始终确保这种情况,而kuddo则要注意警告并采取行动。
Undefined variables can be a source of very hard to track down bugs, because often the value turns out some reasonable value except in 1% of cases. 未定义的变量可能是非常难以跟踪错误的来源,因为除1%的情况外,该值通常会得出一些合理的值。 Because every run of the code is different, it can be difficult to reproduce the bug, let alone diagnose it. 由于每次运行的代码都不相同,因此很难重现该错误,更不用说对其进行诊断了。
This is why Java is insistent. 这就是Java坚持不懈的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.