我必须写一个程序来计算邮寄的东西的运费。
以下是一些规范以及练习运行应该是什么样子:
计算:
用法:发货方法成本隔夜$5*重量两天$2*重量经济$1*重量(是的,这只是=重量)数据验证在此程序中,您还需要在主方法中执行一些验证。明确地:
项目说明不能为空。物品重量必须为
运行#1:未输入项目说明:
输入项目描述:
运行#2:输入的项目重量无效:
输入项目说明:一个大框
运行#3:输入的装运方式无效:
输入物品描述:一个大盒子输入物品重量(磅): 3.5
您希望以多快的速度发货:(O)vernight(T)两天(E)经济(可能需要7天)选择一个选项:P无效的发货方式。程序无法继续
运行#4:生成发票
输入物品描述:一个大盒子输入物品重量(磅): 3.5
您希望以多快的速度发货包裹:(O)vernight(T)两天(E)经济(可能需要7天)选择一个选项:O
***我们装运发票***项目描述:一个大箱子项目重量:3.50装运方法:O总成本:$17.50
分解您必须使用下面提供的分解。为了最大限度地保持理智,请记住在开发模式下一次构建和测试一个方法。其中两个方法已经为您编写,并作为本练习的代码模板的一部分提供。
用法:double getWallHeight(扫描仪scnr)-此方法从主方法调用。它接受扫描仪作为参数,提示用户输入墙高度,并将用户输入的值返回给main方法。
使用:双getWallWidth(扫描仪scnr)-此方法是从main方法调用的。它接受扫描仪作为参数,提示用户墙壁高度,并将用户输入的值返回给main方法。
用法:double CalcWallRea(双墙高,双墙宽)-此方法从主方法调用。它接受墙高和墙宽,并以平方英尺为单位计算墙面积。
用法:double calcGallons(double wallArea)-此方法从主方法调用。它接受墙面积,并确定整个房间需要多少油漆。
用法:void displayResults(需要双wallArea、双gallonspaintResults)-此方法从主方法调用。它接受瓦莱拉和油漆房间所需的油漆量。它以整数形式向用户显示结果,包括行走区域和需要购买的油漆罐数量。
这是我的密码:
import java.util.*;
import java.util.Scanner;
public class WeShipIt {
public static final int OVERNIGHT_CHARGE = 5;
public static final int TWO_DAY_CHARGE = 2;
public static final int ECONOMY_CHARGE = 1;
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in); //scanner object to pass around
double weight;
String itemDescription;
char shipMethod;
double shippingCost;
itemDescription = getitemDescription(keyboard);
weight = getShipWeight(keyboard);
shipMethod = getShipClass(keyboard);
shippingCost = calculateShipping(shipMethod, weight);
if (itemDescription.length() == 0){
System.out.println("Invalid description. Program cannot continue");
System.exit(0);
} else {
getShipWeight(keyboard);
if (weight <= 0){
System.out.println("Invalid shipping weight. Program cannot continue");
} else {
getShipClass(keyboard);
if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')){
System.out.println("Invalid shipping method. Program cannot continue");
} else {
displayResults(itemDescription, weight, shipMethod, shippingCost);
}
}
}
}
//get item description
public static String getitemDescription(Scanner keyboard){
System.out.println("Enter item description:");
String itemDescription = keyboard.next();
return itemDescription;
}
//get item weight
public static double getShipWeight(Scanner console){
System.out.println("Enter item weight in lbs:");
double itemWeight = console.nextDouble();
return itemWeight;
}
//get user's choice for shipping method and return it
public static char getShipClass(Scanner keyboard) {
char shipMethod;
//get shipping method
System.out.println();
System.out.println("How would you like to ship your package:");
System.out.println("(O)vernight");
System.out.println("(T)wo Days");
System.out.println("(E)conomy (may take up to 7 days)");
System.out.println("Choose an option: ");
shipMethod = keyboard.next().charAt(0); //Prof. Dan says leave this line in here. Will explain in
class.
shipMethod = Character.toUpperCase(shipMethod);
return shipMethod;
}
//calculate and return shipping charge
public static double calculateShipping(char shipMethod, double weight){
double shipCharge;
if (shipMethod == 'O') {
shipCharge = weight * OVERNIGHT_CHARGE;
}
else if (shipMethod == 'T') {
shipCharge = weight * TWO_DAY_CHARGE;
}
else if (shipMethod == 'E') {
shipCharge = weight * ECONOMY_CHARGE;
} else {
shipCharge = 0;
}
return shipCharge;
}
//display shipping charge invoice
public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double
shipCost) {
System.out.println();
System.out.println("*** WE SHIP INVOICE ****");
System.out.println("Item Description: " + itemDescription);
System.out.printf("Item Weight: %.2f\n" + shipWeight);
System.out.println("Ship Method: " + shipMethod);
System.out.printf("Total Cost: $%.2f\n" + shipCost);
}
}
这是我使用“box 3.5 0”作为输入时的输出:
输入项目说明:输入项目重量(磅):
您希望如何运送包裹:(O)vernight(T)两天(E)经济(可能需要7天)选择一个选项:输入物品重量(磅):
我不需要重新打印输入,但我很困惑为什么它再次要求项目的重量,而不打印出最终结果。
我也得到这个错误:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at WeShipIt.getShipWeight(WeShipIt.java:67)
at WeShipIt.main(WeShipIt.java:35)
我已经试了几个小时来自己解决这个问题,但我请求帮助作为最后手段。我是计算机科学的新手,可能犯了一些愚蠢的错误。非常感谢。
您显示的代码有2个错误:
1:System.out.printf(“项目重量:%.2f\n”装运重量)
和System.out.printf(“总成本:$%.2f\n”发货成本)
执行此操作将引发MissingFormatArgumentException
原因当使用printf并声明参数时,它会使用在逗号之后传递给它的第一个变量(要打印的值)自我调整。在本例中,没有任何逗号,因此它不会识别并抛出
MissingFormatArgumentException
。
相反,你应该这样写:
System.out.printf("Item Weight: %.2f\n",shipWeight);
System.out.printf("Total Cost: $%.2f\n", shipCost);
2:您正在调用
getShipWeight(键盘)
和getShipClass(键盘)
两次第一次
,当用户刚进入程序时第二次
,在else语句中<这迫使您再次输入重量和装运方式。
以下是最后的变化:
public static final int OVERNIGHT_CHARGE = 5;
public static final int TWO_DAY_CHARGE = 2;
public static final int ECONOMY_CHARGE = 1;
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in); //scanner object to pass around
double weight;
String itemDescription;
char shipMethod;
double shippingCost;
itemDescription = getitemDescription(keyboard);
weight = getShipWeight(keyboard);
shipMethod = getShipClass(keyboard);
shippingCost = calculateShipping(shipMethod, weight);
if (itemDescription.length() == 0){
System.out.println("Invalid description. Program cannot continue");
System.exit(0);
} else {
// getShipWeight(keyboard); <-- this is forcing uset to enter weight again.
if (weight <= 0){
System.out.println("Invalid shipping weight. Program cannot continue");
} else {
// getShipClass(keyboard); <-- this is forcing user to again give shipping method
if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')){
System.out.println("Invalid shipping method. Program cannot continue");
} else {
displayResults(itemDescription, weight, shipMethod, shippingCost);
}
}
}
}
//get item description
public static String getitemDescription(Scanner keyboard){
System.out.println("Enter item description:");
String itemDescription = keyboard.next();
return itemDescription;
}
//get item weight
public static double getShipWeight(Scanner console){
System.out.println("Enter item weight in lbs:");
double itemWeight = console.nextDouble();
return itemWeight;
}
//get user's choice for shipping method and return it
public static char getShipClass(Scanner keyboard) {
char shipMethod;
//get shipping method
System.out.println();
System.out.println("How would you like to ship your package:");
System.out.println("(O)vernight");
System.out.println("(T)wo Days");
System.out.println("(E)conomy (may take up to 7 days)");
System.out.println("Choose an option: ");
shipMethod = keyboard.next().charAt(0); //Prof. Dan says leave this line in here. Will explain in
shipMethod = Character.toUpperCase(shipMethod);
return shipMethod;
}
//calculate and return shipping charge
public static double calculateShipping(char shipMethod, double weight){
double shipCharge;
if (shipMethod == 'O') {
shipCharge = weight * OVERNIGHT_CHARGE;
}
else if (shipMethod == 'T') {
shipCharge = weight * TWO_DAY_CHARGE;
}
else if (shipMethod == 'E') {
shipCharge = weight * ECONOMY_CHARGE;
} else {
shipCharge = 0;
}
return shipCharge;
}
//display shipping charge invoice
public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double
shipCost) {
System.out.println();
System.out.println("*** WE SHIP INVOICE ****");
System.out.println("Item Description: " + itemDescription);
System.out.printf("Item Weight: %.2f\n",shipWeight); // <-- changed
System.out.println("Ship Method: " + shipMethod);
System.out.printf("Total Cost: $%.2f\n", shipCost); // <-- changed
}
输出:
Enter item description:
box
Enter item weight in lbs:
3.5
How would you like to ship your package:
(O)vernight
(T)wo Days
(E)conomy (may take up to 7 days)
Choose an option:
O
*** WE SHIP INVOICE ****
Item Description: box
Item Weight: 3.50
Ship Method: O
Total Cost: $17.50
很好,你正在学习java。
我们的计划需要做一点小小的改变<代码>获取装运重量
请使用此代码
import java.util.Scanner;
public class WeShipIt {
public static final int OVERNIGHT_CHARGE = 5;
public static final int TWO_DAY_CHARGE = 2;
public static final int ECONOMY_CHARGE = 1;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); // scanner object to pass around
double weight;
String itemDescription;
char shipMethod;
double shippingCost;
itemDescription = getitemDescription(keyboard);
/*weight = getShipWeight(keyboard);
shipMethod = getShipClass(keyboard);
shippingCost = calculateShipping(shipMethod, weight);*/
if (itemDescription.length() == 0) {
System.out.println("Invalid description. Program cannot continue");
System.exit(0);
} else {
weight = getShipWeight(keyboard);
if (weight <= 0) {
System.out.println("Invalid shipping weight. Program cannot continue");
} else {
shipMethod =getShipClass(keyboard);
shippingCost = calculateShipping(shipMethod, weight);
if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')) {
System.out.println("Invalid shipping method. Program cannot continue");
} else {
displayResults(itemDescription, weight, shipMethod, shippingCost);
}
}
}
}
//get item description
public static String getitemDescription(Scanner keyboard) {
System.out.println("Enter item description:");
String itemDescription = keyboard.next();
return itemDescription;
}
//get item weight
public static double getShipWeight(Scanner console) {
System.out.println("Enter item weight in lbs:");
double itemWeight = console.nextDouble();
return itemWeight;
}
// get user's choice for shipping method and return it
public static char getShipClass(Scanner keyboard) {
char shipMethod;
// get shipping method
System.out.println();
System.out.println("How would you like to ship your package:");
System.out.println("(O)vernight");
System.out.println("(T)wo Days");
System.out.println("(E)conomy (may take up to 7 days)");
System.out.println("Choose an option: ");
shipMethod = keyboard.next().charAt(0); // Prof. Dan says leave this line in here. Will explain in class.
shipMethod = Character.toUpperCase(shipMethod);
return shipMethod;
}
// calculate and return shipping charge
public static double calculateShipping(char shipMethod, double weight) {
double shipCharge;
if (shipMethod == 'O') {
shipCharge = weight * OVERNIGHT_CHARGE;
} else if (shipMethod == 'T') {
shipCharge = weight * TWO_DAY_CHARGE;
} else if (shipMethod == 'E') {
shipCharge = weight * ECONOMY_CHARGE;
} else {
shipCharge = 0;
}
return shipCharge;
}
// display shipping charge invoice
public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double shipCost) {
System.out.println();
System.out.println("*** WE SHIP INVOICE ****");
System.out.println("Item Description: " + itemDescription);
System.out.println("Item Weight: "+ shipWeight);
System.out.println("Ship Method: " + shipMethod);
System.out.println("Total Cost: $"+ shipCost);
}
}
实际上,您已经在else块中调用了“getShipWeight(键盘)”和“getShipClass(键盘)”,这就是它再次请求权重的原因。只要对这些语句进行注释,代码就会正常运行。
我试图在java应用程序中调用java存储过程。我正在使用ORACLE数据库和JDeveloper。 我得到错误"异常线程"主"java.lang.NullPointerException.我不知道我做错了什么。 我有一个表“Beer”,我想用一个存储过程来选择所有数据,我从Java应用程序中调用这个存储过程。 我有一个java.class文件Store_A. java,我已经加载到ORACLE数
我的代码中出现了这个错误。 这是我的代码: 这就是结果。错误:在线程“main”java中输入model:Exception。lang.NullPointerException在汽车上。主(车.java:10)
问题内容: 这是代码: 错误: 跑: 请帮助我解决此错误。我正在使用Windows Vista OS。 谁能建议我如何以.3gp格式存储文件?请帮助 问题答案: 方法内部的第202行的某些对象引用是在代码尝试使用点运算符访问/调用它时进行的。 例如 解决方案实际上很简单。只需实例化它即可确保它不为空: …或在访问/调用之前简单地执行nullcheck:
我刚开始我的大学java课程,在扫描器类中不断得到这个错误。 我不断得到的错误是:
我对Java非常陌生,似乎遇到了一些奇怪的错误。我到处寻找解决方案,我遇到的所有解决方案都与我已有的完全相同。 我已经编写了一个类,将目的地添加到ArrayList,但它不起作用。 我得到了这个错误:“线程中的异常”main“java.lang.NullPointerException” 这是我的代码: 我试图添加到ArrayList的数据的代码是这样的: 它退出方法中的程序,并且不将目标添加到数
我在我新更新的OS X(现在是Mavericks)中启动了。 现在在Android Studio(也刚刚更新顺便说一句)我收到一条消息 错误:异常生成进程终止:线程“main”java中出现异常。lang.NoSuchMethodError:org。阿帕奇。log4j。PropertyConfiguration。配置(Ljava/io/InputStream;)V在org。喷气式飞机。jps。cm