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語句
你應該閱讀本http://www.java-made-easy.com /variable-scope.html – nem035 2014-10-09 13:32:26
原因是你的int如果只在塊內可見,如果您希望basePrice對您的整個主要方法可見,請將其聲明在您聲明所有重量和價格數據的位置。 – SMA 2014-10-09 13:34:08
在if語句內部聲明一個'int basePrice = 0;',如果並且只做一個'basePrice = 17,21 ...'。 – Milaci 2014-10-09 13:35:21