不要在if语句中显示声明的int

时间:2014-10-09 13:29:47

标签: java if-statement

package assignment_3_1;
import java.util.Scanner;
public class Assignment_3_1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    //create a scanner
    Scanner input = new Scanner (System.in);

    //obtain package weight 1
    System.out.print("Enter the Package Weight (In Pounds): ");
    int packageWeight1 = input.nextInt();

    double WeightCalc1  = 5;
    double WeightCalc2  = 15;
    double WeightCalc3  = 34;
    double WeightCalc4  = 45;
    double WeightCalc5  = 60;
    double WeightCalc6  = 60;
    double priceA = 12;
    double priceB = 14;
    double priceC = 17;
    double priceD = 21;
    double priceE = 33;
    double priceF = 105;


    //if WeightCalc1 >= packageWeight1 the base charge is 12

   if (WeightCalc1 >= packageWeight1)
   {
       System.out.println("The Base Charge is :  " + priceA);
       int basePrice = 12;
   }

   else
   {
        //if WeightCalc2 >= packageWeight1 the base charge is 14
        if (WeightCalc2 >= packageWeight1)
        {
            System.out.println("The Base Charge is: " + priceB);
            int basePrice = 14;
        }

        else
        {
                //if WeightCalc3 >= packageWeight1 the base charge is 17
                if (WeightCalc3 >= packageWeight1)
                {
                    System.out.println("The Base Charge is: " + priceC);
                    int basePrice = 17;
                }

                else
                {
                    //if weightCalc4 >= packageWeight1 the base charge is 21
                    if (WeightCalc4 >= packageWeight1)
                    {
                        System.out.println("The base charge is: " + priceD);
                        int basePrice = 21;
                    }
                    else
                    {
                        //if weightCalc5 >= packageWeight1 the base charge is 33
                        if (WeightCalc5 >= packageWeight1)
                        {
                            System.out.println("The base charge is: " + priceE );
                            int basePrice = 33;
                        }
                        else
                        {
                            //if weightCalc6 < packageWeight1 the base charge is 105
                            if (WeightCalc6 < packageWeight1)
                            {
                                System.out.println("The base charge is: " + priceF);
                                int basePrice = 105;
                            }
                            else
                            {
                                System.out.println("Re-Run the Program");
                            }
                        }
                    }
                }

        }


   }
   //obtain zipCode
   System.out.println("Enter your 5 Digit Zip Code: ");
   int zipCode = input.nextInt();

    double perc1 = 3999; 
    double perc2 = 5000;
    double perc3 = 5999;
    double perc4 = 7000;

    //if perc1 < basePrice < perc2
    if (perc1 < basePrice < perc2)
    {

    }

}
}

我在if语句中声明了一个int,当我开始在大if语句之后编写底部部分时我尝试使用basePrice在if语句中声明的int我尝试更改int的名称并使用double而不是int,没有什么可以确定我做错了什么。

8 个答案:

答案 0 :(得分:1)

在java中,变量的范围是块的本地。块通常是{...}之间的代码。如果你需要在粗体旁边使用它,那么你应该在块之外声明它。有关详细信息,请参阅示例。

if(condition){
   int i=10;
}

你不能在if之外使用我。

如果你愿意,你应该做点什么

int i =0;
if(condition)
{
   i=10;
}

现在您可以在if。

之外使用它

答案 1 :(得分:1)

问题是每个int basePrice都在它自己的范围内声明,并且不存在于该范围之外。你想用所有的双打声明int,如下所示:

double WeightCalc1  = 5;
double WeightCalc2  = 15;
double WeightCalc3  = 34;
double WeightCalc4  = 45;
double WeightCalc5  = 60;
double WeightCalc6  = 60;
double priceA = 12;
double priceB = 14;
double priceC = 17;
double priceD = 21;
double priceE = 33;
double priceF = 105;
// declare basePrice here
int basePrice;

然后没有这些块:

   //if WeightCalc1 >= packageWeight1 the base charge is 12
   // base price only exists within these brackes

   if (WeightCalc1 >= packageWeight1)
   {
    // base price only exists within these brackes
       System.out.println("The Base Charge is :  " + priceA);
       int basePrice = 12;
   }

您改为:

   //if WeightCalc1 >= packageWeight1 the base charge is 12
   if (WeightCalc1 >= packageWeight1)
   {
       System.out.println("The Base Charge is :  " + priceA);
       basePrice = 12;
   }

答案 2 :(得分:0)

变量的范围仅限于此。

如果条件允许,你无法访问它。

您可以将其声明为全局,以便在另一个中访问它。

<强>更新

int basePrice = 0;  // declare

之后在其他if条件中赋值。

 if (WeightCalc1 >= packageWeight1)
   {
       System.out.println("The Base Charge is :  " + priceA);
      // int basePrice = 12;  //--not like this
      basePrice = 12;   // assign
   }

答案 3 :(得分:0)

在开头声明int basePrice(例如使用packageWeight1),将其初始化为某个值(0?)然后将所有int basePrice = ...更改为basePrice = ...

if语句完成后,执行内部声明的任何值都将丢失。

答案 4 :(得分:0)

basePriceif语句的范围内定义。将其移到声明范围之外

int basePrice = 12;

if (weightCalc1 >= packageWeight1) {
  ...

还有声明

if (perc1 < basePrice < perc2) {

不会编译。也许你的意思是

if (perc1 < basePrice && basePrice < perc2) {

答案 5 :(得分:0)

在if之外声明它,因为如果只是

,你将其范围限制在那个范围内
package assignment_3_1;
import java.util.Scanner;
public class Assignment_3_1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    //create a scanner
    Scanner input = new Scanner (System.in);

    //obtain package weight 1
    System.out.print("Enter the Package Weight (In Pounds): ");
    int packageWeight1 = input.nextInt();

    double WeightCalc1  = 5;
    double WeightCalc2  = 15;
    double WeightCalc3  = 34;
    double WeightCalc4  = 45;
    double WeightCalc5  = 60;
    double WeightCalc6  = 60;
    double priceA = 12;
    double priceB = 14;
    double priceC = 17;
    double priceD = 21;
    double priceE = 33;
    double priceF = 105;
       int basePrice = 12; // declare it here and it will work
    //if WeightCalc1 >= packageWeight1 the base charge is 12

   if (WeightCalc1 >= packageWeight1)
   {
       System.out.println("The Base Charge is :  " + priceA);

   }

答案 6 :(得分:0)

代码与basePrice有问题。您在basePrice内宣布if&amp; else你正在外面使用它。尝试在main方法中声明为全局变量,以便您可以从main中的任何位置进行访问。

public static void main(String[] args) {

    //create a scanner
    Scanner input = new Scanner (System.in);
    int basePrice = 0;

    // Your rest of code will be here ....
}

答案 7 :(得分:0)

basePrice必须在主

的开头声明

以下是一些重构:

private static Integer checkWeight(int packageWeight, int weightCalc, int price)
{
    if (weightCalc >= packageWeight)
    {
        System.out.println("The Base Charge is: " + price);
        return price;
    }
    else
    {
        return null;
    }
}

public static void main(String[] args)
{
    //create a scanner
    Scanner input = new Scanner (System.in);

    //obtain package weight 1
    System.out.print("Enter the Package Weight (In Pounds): ");
    int packageWeight1 = input.nextInt();

    int[] weightCalcs = {5, 15, 34, 45, 60, 60};
    int[] prices = {12, 14, 17, 21, 33, 105};

    Integer basePrice = null;

    for (int i = 0; i < weightCalcs.length; i++)
    {
        if ((basePrice = checkWeight(packageWeight1, weightCalcs[i], prices[i])) != null)
        {
            // price found !
            break;
        }
    }

    if (basePrice != null)
    {
       //obtain zipCode
       System.out.println("Enter your 5 Digit Zip Code: ");
       int zipCode = input.nextInt();

        double perc1 = 3999; 
        double perc2 = 5000;
        double perc3 = 5999;
        double perc4 = 7000;

        // TODO using basePrice for the next intructions
    }
    else
    {
        System.out.println("Re-Run the Program");
    }
}
相关问题