2013-10-12 44 views
0

这是说我的本地变量newaccbalance可能尚未初始化。我知道我宣布它是一个双。请帮助Dr Java代码错误

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

    } 
} 
+0

该代码不可读。请编辑:) – tianz

+0

@CoderTian我刚刚做到了!您还可以提交需要改进的问题的编辑建议。 –

+0

这是我第一次使用这个网站,我不是很擅长它 – user2874840

回答

3

首先,声明和初始化不是一回事。

double newaccbalance;声明变量。

newaccbalance = 42;初始化变量。

在你的代码的问题是,编译器不能保证您的if语句将是真实的,因此有可能newaccbalance要留给初始化。

我建议两两件事:

首先,初始化变量的默认值,double newaccbalance = 0;都将声明和初始化变量。

其次,改变你的if语句的结构,还可以使用的if-else-如果是这样的:

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! 
} 
+0

那么应该怎样编码? – user2874840

+0

我该怎么办? – user2874840

+0

当你声明它时给你的变量一些默认值。 – ArniDat

1

您是声明您的变量。你需要初始化你的变量

声明是创建变量:

double newaccbalance; 

初始化是你分配一个变量的值:

newaccbalance = 0; 

所以,你需要做的是:

double newaccbalance = 0.0; 
0

您的所有作业都包含在if控制结构中。如果没有的条件评估为true,变量将保持未分配状态。作为local变量,它也不会获得默认值。

那就是为什么有消息称,这可以将未初始化。

0

它没有被初始化。当你声明它尝试double newaccbalance = 0.0;

我认为问题是newaccbalance只是有条件地设置(在if语句中),所以它永远不能保证被设置为一个值。

初始化和声明是两回事。

0

我不认为你得到一个错误,但一个警告。
无论如何,Java是正确的。
您的变量newaccbalance可能尚未初始化。

你已经声明它为double,但你只在if语句中赋值。
Java不知道这些if语句是否覆盖了所有可能的情况,因此会警告您实际上可能未分配的内容为newaccbalance

请注意,Java不会为未定义的变量分配零值。
你必须自己做。

更改顶部声明:该

double newaccbalance = 0; //Or whatever default value you want. 

要么或增加额外的else背后的最后一个像这样:

else if (acctype == 2 && currentbalance < minbalance){ 
    newaccbalance = (currentbalance - 10); 
} 
else { newaccbalance = 0; } 

这将确保该编译器的满意度newaccbalance已定义价值,而不是随机的。
你应该始终确保情况是这样,并且kuddo听取警告并采取行动。
未定义的变量可能是一个很难追踪错误的来源,因为除了1%的情况外,通常该值会产生一些合理的值。因为每次运行的代码都不同,所以很难重现错误,更不用说诊断它了。

这就是Java坚持的原因。

+0

我做到了,但它仍然没有运行该变量通过if语句并返回一个新值,它只是表示它仍为零 – user2874840