2014-10-09 60 views
1
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) 
    { 

    } 

} 
} 

我宣布里面一个int if语句,当我开始后的大写入底部if语句我试着用basePrice我if语句我”的内部声明中的int我尝试改变int的名称,并使用double而不是int,没有任何工作不知道我做错了什么。惯于显示INT宣布if语句

+0

你应该阅读本http://www.java-made-easy.com /variable-scope.html – nem035 2014-10-09 13:32:26

+0

原因是你的int如果只在块内可见,如果您希望basePrice对您的整个主要方法可见,请将其声明在您声明所有重量和价格数据的位置。 – SMA 2014-10-09 13:34:08

+0

在if语句内部声明一个'int basePrice = 0;',如果并且只做一个'basePrice = 17,21 ...'。 – Milaci 2014-10-09 13:35:21

回答

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; 
    } 
0

变量的范围仅限于此。

如果条件不满意,您无法访问它。

你可以声明它是全局的,以在另一个if中访问它。

更新

int basePrice = 0; // declare 

,而且如果条件在指定其他值之后。

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

感谢您的帮助,有什么办法可以让它永久保存,因此我可以使用它if语句 – 2014-10-09 13:35:06

+0

@GarrettCarder初始化if语句之外的变量,使其能够在if外部访问if 。然后,您只需在if内设置值。 – Grice 2014-10-09 13:40:52

+0

我该如何声明它全球 – 2014-10-09 18:21:54

0

声明开头(与packageWeight1为例)INT basePrice,它初始化到某一值(0?)然后改变所有的int basePrice = ...basePrice = ...

一旦if语句执行完毕的任何值内部宣告失踪。

0

basePrice被定义在if陈述的范围内。移动此声明

int basePrice = 12; 

if (weightCalc1 >= packageWeight1) { 
    ... 

而且声明

if (perc1 < basePrice < perc2) { 

不会编译的范围之内。也许你的意思

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

声明它之外,如果因为u的,如果限制其范围只

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

    } 
1

在Java中变量的作用域是本地的一个块。块通常是{...}之间的代码。如果你需要将它用在粗体上,那么你应该在块之外声明它。详情请参阅示例。

if(condition){ 
    int i=10; 
} 

你不能在if之外使用我。

如果你想在你应该这样做

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

现在你可以使用它,如果外面。

0

代码问题与basePrice。您在if & else之内声明basePrice,并且您在其外部使用它。尝试在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 .... 
} 
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"); 
    } 
}