“线程中的异常”;“主要”;java.util.NoTouchElementException错误,程序正在打印错误的结果

“线程中的异常”;“主要”;java.util.NoTouchElementException错误,程序正在打印错误的结果,java,Java,我必须写一个程序来计算邮寄的东西的运费 以下是一些规范以及练习运行应该是什么样子: 计算: 用途:船法成本 隔夜$5*重量 两天$2*重量 经济型$1*重量(是的,这只是=重量) 在这个程序的数据验证中,您还需要在main方法中执行一些验证。具体而言: 项目说明不能为空。 物品重量必须大于0。 装运方法必须是O、T或E。程序应接受小写或大写的等效值。 运行示例: 确保以println结束输出 运行#1:未输入项目说明: 输入项目说明: 实际上,您已经在else块中调用了“getShipWeigh

我必须写一个程序来计算邮寄的东西的运费

以下是一些规范以及练习运行应该是什么样子:

计算:

用途:船法成本 隔夜$5*重量 两天$2*重量 经济型$1*重量(是的,这只是=重量) 在这个程序的数据验证中,您还需要在main方法中执行一些验证。具体而言:

项目说明不能为空。 物品重量必须大于0。 装运方法必须是O、T或E。程序应接受小写或大写的等效值。 运行示例: 确保以println结束输出

运行#1:未输入项目说明:

输入项目说明:
实际上,您已经在else块中调用了“getShipWeight(键盘)”和“getShipClass(键盘)”,这就是它再次请求权重的原因。只要对这些语句进行注释,您的代码就会正常运行。

很好,您正在学习java

我们的计划需要做一点小小的改变<代码>getShipWeight和
getShipClass
在实际代码中被调用两次

请使用此代码

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);
    }
}
import java.util.Scanner;
公共类WeShipIt{
公共静态最终整夜费用=5;
公共静态最终int两天费用=2;
公共静态最终综合经济费用=1;
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);//要传递的扫描仪对象
双倍重量;
字符串项描述;
炭沉淀法;
双重运输成本;
itemDescription=getitemDescription(键盘);
/*重量=getShipWeight(键盘);
shipMethod=getShipClass(键盘);
装运成本=计算装运(装运方法、重量)*/
if(itemDescription.length()=0){
System.out.println(“无效说明。程序无法继续”);
系统出口(0);
}否则{
重量=getShipWeight(键盘);

如果(重量显示的代码有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
     }

这回答了你的问题吗?
System.out.printf("Item Weight: %.2f\n",shipWeight);
System.out.printf("Total Cost: $%.2f\n", shipCost);
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