在两个 if 语句中使用 Java 中的变量
Using variables in Java within two if statements
所以我目前正在编写一个程序,要求用户提供他们将乘以的多项式的次数,然后再乘以所述多项式。我的程序设置为只乘以三次多项式。我使用 if 语句来检查次数,然后提示用户输入所需的系数数。但是,我在程序中还有另一个 if 语句,它使用这些系数并将它们分配给乘积多项式的系数。它说我不能使用这个变量,因为它可能还没有被初始化??我在代码的开头声明了所有变量,而不是在 if 语句中,但它仍然说它可能尚未初始化。我将在下面包含代码。如有任何帮助,我们将不胜感激!
if (degreeform == 1)
{
degreeformstate = true;
coef_A = conIO.promptForInt("Enter coefficient a: ");
conIO.printEndline();
coef_B = conIO.promptForInt("Enter coefficient b: ");
conIO.printEndline();
coef_C = conIO.promptForInt("Enter coefficient c: ");
conIO.printEndline();
coef_D = conIO.promptForInt("Enter coefficient d: ");
conIO.printEndlines(TWO_ENDLINES);
}
else if (degreeform == 2)
{
degreeformstate = true;
coef_A = conIO.promptForInt("Enter coefficient a: ");
conIO.printEndline();
coef_B = conIO.promptForInt("Enter coefficient b: ");
conIO.printEndline();
coef_C = conIO.promptForInt("Enter coefficient c: ");
conIO.printEndline();
coef_D = conIO.promptForInt("Enter coefficient d: ");
conIO.printEndline();
coef_E = conIO.promptForInt("Enter coefficient e: ");
conIO.printEndlines(TWO_ENDLINES);
}
else if (degreeform == 3)
{
degreeformstate = true;
coef_A = conIO.promptForInt("Enter coefficient a: ");
conIO.printEndline();
coef_B = conIO.promptForInt("Enter coefficient b: ");
conIO.printEndline();
coef_C = conIO.promptForInt("Enter coefficient c: ");
conIO.printEndline();
coef_D = conIO.promptForInt("Enter coefficient d: ");
conIO.printEndline();
coef_E = conIO.promptForInt("Enter coefficient e: ");
conIO.printEndline();
coef_F = conIO.promptForInt("Enter coefficient f: ");
conIO.printEndlines(TWO_ENDLINES);
}
else
{
degreeformstate = false;
conIO.printString("Incorrect number of polynomial degrees entered - Program Aborted");
System.exit(0);
}
if (degreeform == 1)
{
coef_V = coef_A * coef_C;
coef_W = (coef_B * coef_C) + (coef_A * coef_D);
coef_X = (coef_B * coef_D);
}
// polynomial type 2
else if (degreeform == 2)
{
coef_V = coef_A * coef_C;
coef_W = (coef_B * coef_C) + (coef_A * coef_D);
coef_X = (coef_A * coef_E) + (coef_B * coef_D);
coef_Y = coef_B * coef_E;
}
// polynomial type 3
else
{
coef_V = coef_A * coef_C;
coef_W = (coef_B * coef_C) + (coef_A * coef_D);
coef_X = (coef_A * coef_E) + (coef_B * coef_D);
coef_Y = (coef_A * coef_F)+ (coef_B * coef_E);
coef_Z = coef_B * coef_E;
}
您不必声明,而是在代码开头或 else 块中将变量初始化为其默认值。
所以我目前正在编写一个程序,要求用户提供他们将乘以的多项式的次数,然后再乘以所述多项式。我的程序设置为只乘以三次多项式。我使用 if 语句来检查次数,然后提示用户输入所需的系数数。但是,我在程序中还有另一个 if 语句,它使用这些系数并将它们分配给乘积多项式的系数。它说我不能使用这个变量,因为它可能还没有被初始化??我在代码的开头声明了所有变量,而不是在 if 语句中,但它仍然说它可能尚未初始化。我将在下面包含代码。如有任何帮助,我们将不胜感激!
if (degreeform == 1)
{
degreeformstate = true;
coef_A = conIO.promptForInt("Enter coefficient a: ");
conIO.printEndline();
coef_B = conIO.promptForInt("Enter coefficient b: ");
conIO.printEndline();
coef_C = conIO.promptForInt("Enter coefficient c: ");
conIO.printEndline();
coef_D = conIO.promptForInt("Enter coefficient d: ");
conIO.printEndlines(TWO_ENDLINES);
}
else if (degreeform == 2)
{
degreeformstate = true;
coef_A = conIO.promptForInt("Enter coefficient a: ");
conIO.printEndline();
coef_B = conIO.promptForInt("Enter coefficient b: ");
conIO.printEndline();
coef_C = conIO.promptForInt("Enter coefficient c: ");
conIO.printEndline();
coef_D = conIO.promptForInt("Enter coefficient d: ");
conIO.printEndline();
coef_E = conIO.promptForInt("Enter coefficient e: ");
conIO.printEndlines(TWO_ENDLINES);
}
else if (degreeform == 3)
{
degreeformstate = true;
coef_A = conIO.promptForInt("Enter coefficient a: ");
conIO.printEndline();
coef_B = conIO.promptForInt("Enter coefficient b: ");
conIO.printEndline();
coef_C = conIO.promptForInt("Enter coefficient c: ");
conIO.printEndline();
coef_D = conIO.promptForInt("Enter coefficient d: ");
conIO.printEndline();
coef_E = conIO.promptForInt("Enter coefficient e: ");
conIO.printEndline();
coef_F = conIO.promptForInt("Enter coefficient f: ");
conIO.printEndlines(TWO_ENDLINES);
}
else
{
degreeformstate = false;
conIO.printString("Incorrect number of polynomial degrees entered - Program Aborted");
System.exit(0);
}
if (degreeform == 1)
{
coef_V = coef_A * coef_C;
coef_W = (coef_B * coef_C) + (coef_A * coef_D);
coef_X = (coef_B * coef_D);
}
// polynomial type 2
else if (degreeform == 2)
{
coef_V = coef_A * coef_C;
coef_W = (coef_B * coef_C) + (coef_A * coef_D);
coef_X = (coef_A * coef_E) + (coef_B * coef_D);
coef_Y = coef_B * coef_E;
}
// polynomial type 3
else
{
coef_V = coef_A * coef_C;
coef_W = (coef_B * coef_C) + (coef_A * coef_D);
coef_X = (coef_A * coef_E) + (coef_B * coef_D);
coef_Y = (coef_A * coef_F)+ (coef_B * coef_E);
coef_Z = coef_B * coef_E;
}
您不必声明,而是在代码开头或 else 块中将变量初始化为其默认值。