我认为对话框代码没有问题,我想我的zip变量有问题。最初我把它设置为双倍,并试图使用开关进行附加费计算。我将其更改为字符串,然后切换不起作用,所以我将其更改为if / else语句。编译器没有发现任何错误,但现在输入所有其他数据后,我的最终消息对话框不会显示。我发现其他人询问这个任务,但没有一个人需要使用对话框,这就是我的问题所在。这是我的代码,感谢所有帮助。
import javax.swing.JOptionPane; //Needed for JOptionPane
public class EZShippingCompany
{
public static void main (String[] args)
{
String input; //to hold the string input
String zip = new String(); //to hold zip
double weight; //to hold weight
double length; //to hold length
double width; //to hold width
double height; //to hold height
double dimensions;//to hold dimensions
double shippingcost=0; //to hold shipping cost
double surcharge=0; //to hold surcharge
double additionalcharge=0; // to hold additional 2%
double totalshippingcost; // to hold total
//get zip
input = JOptionPane.showInputDialog ("Please enter your zip code");
//get weight
input = JOptionPane.showInputDialog ("Please enter the weight of your shipment");
weight = Double.parseDouble(input);
//get length
input = JOptionPane.showInputDialog ("Please enter the length in inches of your shipment");
length = Double.parseDouble(input);
//get width
input = JOptionPane.showInputDialog ("Please enter the width in inches of your shipment");
width = Double.parseDouble(input);
//get height
input = JOptionPane.showInputDialog ("Please enter the height in inches of your shipment");
height = Double.parseDouble(input);
dimensions = length * width * height;
//calculate shipping cost
{if (weight <= 5)
shippingcost = 12.00;
else if (dimensions + weight >=5.1 && dimensions + weight <= 15)
shippingcost = 14.00;
else if (dimensions + weight >= 15 && dimensions + weight <= 34)
shippingcost = 17.00;
else if (dimensions + weight >= 34 && dimensions + weight <= 45)
shippingcost = 21.00;
else if (dimensions + weight >= 45 && dimensions + weight <= 60)
shippingcost = 33.00;
else if (dimensions + weight > 60)
shippingcost = 105.00; }
//calculate surcharge
int x = zip.charAt(0);
if (x == 4)
surcharge = (.05 * shippingcost);
else if (x == 6)
surcharge = (.09 * shippingcost);
else
surcharge = (.014 * shippingcost);
totalshippingcost = shippingcost + surcharge + additionalcharge;
JOptionPane.showMessageDialog(null, "Zip Code: " + zip +
"\nDimensions: " + dimensions + " square inches" +
"\nSurcharge: " + surcharge +
"\nTotal Shipping Cost: $" + totalshippingcost);
//End the program
System.exit(0);
}
}