2014-10-09 89 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"); 
    } 
}