[英]Initializing a variable Java
I'm writing a simple program with a do while loop and switch, which cna accept a mathematical operation and execute it for given 2 numbers. 我正在编写一个带有do while循环和switch的简单程序,该cna接受数学运算并针对给定的2个数字执行它。
The problem I have is, why should I initialize the result produced by the operation to zero at the beginning. 我的问题是,为什么我应该在开始时将操作产生的结果初始化为零。
If I don't make ans=0, it gives me errors. 如果我不使ans = 0,它将给我错误。 If the given conditions are not met, some code parts are not executed and I don't need "ans". 如果不满足给定的条件,则某些代码部分将不会执行,并且我不需要“ ans”。
package q3;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char operator;
float no1, no2, ans=0; // <-------------- Why should I initialize ans
do {
System.out.println(" Mathematical Operations to be performed :");
System.out.println("\t * Multiplication\n\t / Division\n\t + Addition\n\t - Subtraction");
System.out.println("Press any other character to exit");
System.out.print("Mathematical Operator : ");
operator = input.next().charAt(0);
if (operator == '*' || operator == '/' || operator == '+' || operator == '-') {
System.out.print("Number 1: ");
no1 = input.nextFloat();
System.out.print("Number 2: ");
no2 = input.nextFloat();
switch (operator) {
case '*':
ans = no1 * no2;
break;
case '/':
ans = no1 / no2;
break;
case '+':
ans = no1 + no2;
break;
case '-':
ans = no1 - no2;
break;
}
System.out.println("The answer of " + no1 + operator + no2 + " = " + ans);
}
} while (operator == '*' || operator == '/' || operator == '+' || operator == '-');
}
} }
This is because if no case evaluates to true, the value of ans
will not be set. 这是因为如果不区分大小写,则不会设置ans
的值。 So you cannot use it. 所以你不能使用它。
You can overcome this by adding a default
case, and setting the value of ans
as 0 in that. 您可以通过添加default
大小写并将其中的ans
值设置为0来克服此问题。
Java requires that all local variables are initialised before they are used. Java要求所有局部变量在使用之前都要初始化。
In your print line, you read the value of abs
, but not all control paths set a value to it. 在打印行中,读取abs
的值,但并非所有控制路径都为其设置一个值。 (Even though you think you've covered all possibilities of your switch
given the outer if
, the compiler will not see things that way: some other thread could modify operator
). (即使您认为您已经涵盖了使用外部if
switch
所有可能性,编译器也不会那样看:其他线程可以修改operator
)。
So your IDE / compiler is suggesting that you initialise it at the point of declaration. 因此,您的IDE /编译器建议您在声明时进行初始化。
You should initialize ans=0;
您应该初始化ans=0;
because you didn't have a default
value for ans
, for this you need to initialized it. 因为您没有ans
的default
值,所以您需要对其进行初始化。
But if you add the defualt value you don't need to initialize it like this: 但是,如果添加默认值,则无需像这样初始化它:
...
case '-':
ans = no1 - no2;
break;
default :
ans = someValue;
break;
Well, it could be that none of the case statements apply and as a result ans
would still be un-initialized. 好吧,可能没有任何case语句适用,结果ans
仍未初始化。 And since local variables have to be initialised before they are being used, you get this error. 而且由于局部变量必须在使用之前进行初始化,因此会出现此错误。
If you didnt initialize it, you ans will have a garbage value at first. 如果您没有初始化它,那么ans首先将具有一个垃圾值。
It is not compulsory to initialize it. 初始化它不是强制性的。
But your program will be a better program if you initialize it. 但是,如果将其初始化,则程序将是一个更好的程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.