Java 为什么在if语句中定义double时遇到困难?

Java 为什么在if语句中定义double时遇到困难?,java,if-statement,double,Java,If Statement,Double,我的代码如下所示: if (gasQuality==87){ double subtotal = unleaded * gallonsSold; }else if (gasQuality==89){ double subtotal = unleadedPlus * gallonsSold; }else if (gasQuality==91){ double subtotal = premium * gallonsSold; } 但由于某些原因,编译器以后不会识别“小计”

我的代码如下所示:

if (gasQuality==87){
    double subtotal = unleaded * gallonsSold;
}else if (gasQuality==89){
    double subtotal = unleadedPlus * gallonsSold;
}else if (gasQuality==91){
    double subtotal = premium * gallonsSold;
}
但由于某些原因,编译器以后不会识别“小计”。例如,如果我想对代码后面的小计应用税收,编译器将读取:

找不到符号
符号:可变小计
地点:Paniagua_类发票
最终双倍城市税=.0375*小计;

我做错了什么?

声明变量
小计
并在if/else语句开始之前将其设置为初始值。

声明变量
小计
并在if/else语句开始之前将其设置为初始值。

声明变量
小计
并将其设置为初始值if/else语句开始前的初始值。

声明变量
小计
,并将其设置为if/else语句开始前的初始值。

这是因为。变量存在于声明它们的块中(还有其他规则,因此您希望进一步阅读)。由于第一个
小计
if
块中声明(由
{}
分隔),因此只能在该块内使用它。要解决此问题,您可以尝试在那些
if
语句之前声明
subtotal

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
此外,您还可以使用switch语句代替那些
if-else-if
语句:

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
这是因为。变量存在于声明它们的块中(还有其他规则,因此您希望进一步阅读)。由于第一个
小计
if
块中声明(由
{}
分隔),因此只能在该块内使用它。要解决此问题,您可以尝试在那些
if
语句之前声明
subtotal

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
此外,您还可以使用switch语句代替那些
if-else-if
语句:

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
这是因为。变量存在于声明它们的块中(还有其他规则,因此您希望进一步阅读)。由于第一个
小计
if
块中声明(由
{}
分隔),因此只能在该块内使用它。要解决此问题,您可以尝试在那些
if
语句之前声明
subtotal

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
此外,您还可以使用switch语句代替那些
if-else-if
语句:

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
这是因为。变量存在于声明它们的块中(还有其他规则,因此您希望进一步阅读)。由于第一个
小计
if
块中声明(由
{}
分隔),因此只能在该块内使用它。要解决此问题,您可以尝试在那些
if
语句之前声明
subtotal

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
此外,您还可以使用switch语句代替那些
if-else-if
语句:

double subtotal = 0; // declaration and initialization

if (gasQuality==87) {
     subtotal = unleaded * gallonsSold; // don't declare again
}
else if (gasQuality==89)
     ...
switch (gasQuality) {
    case 87:
        subtotal = ...;
        break;
    case 89:
        subtotal = ...;
        break;
    default:
        break;
}
您有一个可变范围问题。将代码重构为上述内容将允许您稍后在方法中使用
subtotal

您有一个可变范围问题。将代码重构为上述内容将允许您稍后在方法中使用
subtotal

您有一个可变范围问题。将代码重构为上述内容将允许您稍后在方法中使用
subtotal


您有一个可变范围问题。将代码重构为上述内容将允许您稍后在方法中使用
subtotal

您需要在if-else循环之外定义双重subtotal。否则,变量的作用域将在fp-else循环中结束。请尝试以下操作:-

  double subtotal;

 if (gasQuality==87)
    {
            subtotal = unleaded * gallonsSold;
    }
    else if (gasQuality==89)
    {
            subtotal = unleadedPlus * gallonsSold;
    }
    else if (gasQuality==91)
    {
            subtotal = premium * gallonsSold;
    }

您需要在if-else循环外定义双倍小计。否则,变量的作用域将在fp-else循环中结束。请尝试以下操作:-

  double subtotal;

 if (gasQuality==87)
    {
            subtotal = unleaded * gallonsSold;
    }
    else if (gasQuality==89)
    {
            subtotal = unleadedPlus * gallonsSold;
    }
    else if (gasQuality==91)
    {
            subtotal = premium * gallonsSold;
    }

您需要在if-else循环外定义双倍小计。否则,变量的作用域将在fp-else循环中结束。请尝试以下操作:-

  double subtotal;

 if (gasQuality==87)
    {
            subtotal = unleaded * gallonsSold;
    }
    else if (gasQuality==89)
    {
            subtotal = unleadedPlus * gallonsSold;
    }
    else if (gasQuality==91)
    {
            subtotal = premium * gallonsSold;
    }

您需要在if-else循环外定义双倍小计。否则,变量的作用域将在fp-else循环中结束。请尝试以下操作:-

  double subtotal;

 if (gasQuality==87)
    {
            subtotal = unleaded * gallonsSold;
    }
    else if (gasQuality==89)
    {
            subtotal = unleadedPlus * gallonsSold;
    }
    else if (gasQuality==91)
    {
            subtotal = premium * gallonsSold;
    }

您需要在这些块的外部定义小计。它的范围仅限于
{
}
之间的空间。您需要在这些块的外部定义
小计
。它的范围仅限于
{
}
之间的空间。您需要在这些块的
外部定义
小计
。它的范围仅限于
{
}
之间的空间。您需要在这些块的
外部定义
小计
。它的作用域仅限于
{
}

变量作用域之间的空格,请阅读。我刚刚做了,这很有意义。我是新来的,不太擅长编程。那么需要做什么呢?在if块之前在外部初始化subtotal变量,块内部的任何初始化都保留在块的范围内,不能在blockVariable范围外访问,请阅读。我刚刚做了,这很有意义。我是新来的,不太擅长编程。那么需要做什么呢?在if块之前在外部初始化subtotal变量,块内部的任何初始化都保留在块的范围内,不能在blockVariable范围外访问,请阅读。我刚刚做了,这很有意义。我是新来的,不太擅长编程。那么需要做什么呢?在if块之前在外部初始化subtotal变量,块内部的任何初始化都保留在块的范围内,不能在blockVariable范围外访问,请阅读。我刚刚做了,这很有意义。我是新来的,不太擅长编程。那么需要做什么呢?在if块之前在外部初始化subtotal变量,块内部的任何初始化都会保留在块的范围内,并且不能访问