-1

所以我整个下午都在看这段代码,我看不到错误。这是我应该做的:

为送货服务创建一个Delivery类。该类包含包含以下内容的字段:

  • 包含八位数字的交货编号。前四位数字代表年份,后四位数字代表交货编号。
  • 代表交货区域的代码。本地配送为代码 1,长途配送为代码 2。
  • 要交付的物品的重量(以磅为单位)。
  • 送货费用如下:

为该类创建一个构造函数,该构造函数Delivery接受年份、年内交付数量、交付距离代码和包裹重量的参数。施工方确定八位数的送货单号和送货费。还包括一个显示每个Delivery对象字段的方法。将文件另存为 Delivery.java。

接下来,创建一个应用程序,提示用户输入数据以进行交付。不断提示用户输入以下每个值,直到它们有效:

  • 2001 年至 2025 年之间的四位数年份
  • 1 到 9999(含)之间的年份的交货编号
  • 包裹重量在 0.10 磅到 100 磅之间(含)
  • 送货距离代码为 1 或 2

当所有数据条目都有效时,构造一个Delivery对象,然后显示其值。将文件另存为CreateDelivery.java.

所以这是我的送货班

import javax.swing.*;
import java.util.*;


class Delivery 
{
    //variables
    private int year;
    private int deliveryNumber; //deliveryNo
    private double weight;
    private int distanceCode; //code

        //constructor
        //Delivery()
        //{
        //  year = year;
        //  deliveryNumber = deliveryNumber;
        //  weight = weight;
        //  distanceCode = distanceCode;
        //}

    //get year
    public int getYear()
    {
        return year;
    }
    //set year
    public int setYear (int newYear)
    {
        year = newYear;
        return year;
    }

    //get deliveryNumber
    public int getDeliveryNumber()
    {
        return deliveryNumber;
    }
    //set deliveryNumber
    public int setDeliveryNumber (int newDeliveryNumber)
    {
        deliveryNumber = newDeliveryNumber;
        return deliveryNumber;
    }

    //get weight
    public double getWeight()
    {
        return weight;
    }
    //set Weight
    public double setWeight (double newWeight)
    {
        weight = newWeight;
        return weight;
    }   


    //get distanceCode
    public int getDistanceCode()
    {
        return distanceCode;
    }
    //set distanceCode
    public int setDistanceCode (int newDistanceCode)
    {
        distanceCode = newDistanceCode;
        return distanceCode;
    }   

    //calculate fee
    public double displayFees(int distance, double w) //distance = c
    {
        double fees = 0;

    if(distance == 1)
    {
            if(w < 5)
        {
            fees = 12;
        }
        else if((w < 20)&&(w > 5))
        {
            fees = 16.50;
        }
        else if(w > 20)
        {
            fees = 22;
        }
    }
    else if(distance == 2)
    {
        if(w < 5)
        {
            fees = 35;
        }
        else if(w >= 5)
        {
            fees = 47.95;
        }
    }
        return fees;
    }

    //display method
    public void display(int year, int deliveryNumber, double weight, int distanceCode)
        {
        System.out.println("Year: " + year + '\n' 
            + "Delivery Number: " + deliveryNumber + '\n' 
            + "Weight of the package: " + weight + '\n'
            + "Delivery code: " + distanceCode);
        }
}

这是我的 CreateDelivery 类

 import javax.swing.JOptionPane;
import java.util.Scanner;

public class CreateDelivery
    {
        public static void main(String []args)

        {

            Delivery delivery1 = new Delivery();

            //scanner year
            Scanner input = new Scanner(System.in);
            System.out.print("Please enter the current year, format (yyyy) >>> ");
            delivery1.setYear(input.nextInt());

            //loop year
                    while((delivery1.getYear() <= 2000)||(delivery1.getYear() >= 2026))
                    {
                        System.out.println('\n'+ "Error, year should be in the range of (2010 - 2025). Please enter a valid option >> ");
                        delivery1.setYear(input.nextInt());
                    }

            //scanner for delivery number
            System.out.print('\n'+ "Please enter a delivery number: ");
            delivery1.setDeliveryNumber(input.nextInt());

            //loop for delivery number
                    while((delivery1.getDeliveryNumber() <= 0001)||(delivery1.getDeliveryNumber() >= 10000))
                    {
                        System.out.println("Error, the delivery number is a 4 digit number between 0001 and 9999, please enter a valid option >> ");
                        delivery1.setDeliveryNumber(input.nextInt());
                    }

            //scanner for weight
            System.out.print("Please enter the weight of the package (in pounds): ");
            delivery1.setWeight(input.nextDouble());

                    //loop for weight
                    while((delivery1.getWeight() <= .09)||(delivery1.getWeight() >= 101))
                    {
                        System.out.println("Error, the minimum allowed weight is 0.10 pounds and the maximum is 100 pounds. Please enter a valid weight >> ");
                        delivery1.setWeight(input.nextDouble());
                    }

            //scanner for delivery code
            System.out.print("Please enter 1 for local or 2 for long distance deliveries >> ");
            delivery1.setDistanceCode(input.nextInt());

                    //loop for delivery code
                    while((delivery1.getDistanceCode() == 1) && (delivery1.getDistanceCode() == 2))
                    {
                        System.out.println("Error, please enter a valid distance code (1 for local deliveries and 2 for long distance deliveries) >> ");
                        delivery1.setDistanceCode(input.nextInt());
                    }

            //turn int to string
            String n = Integer.toString(delivery1.getDeliveryNumber());
            String y = Integer.toString(delivery1.getYear());

            String trackingNumber = n + y;
            System.out.println(delivery1.getDistanceCode() + " " 
            + trackingNumber + " " + delivery1.getWeight() + " " + fees);

    }

}

所以我做了你们建议的更改,但现在我无法从 Delivery 课程中收取费用。有什么想法吗?

4

3 回答 3

1

我认为您在 while 循环中的 等方法()的末尾缺少。并且您还在末尾使用未声明的变量,例如,等。getDeliveryNumbergetYeargetDeliveryNumbergetYear

于 2013-06-23T04:09:05.687 回答
1
Delivery()
    {
        year = year;
        deliveryNumber = deliveryNumber;
        weight = weight;
        distanceCode = distanceCode;
    }

将其替换为以下内容:

Delivery(int year, int deliveryNumber, int weight, int distanceCode)
    {
        this.year = year;
        this.deliveryNumber = deliveryNumber;
        this.weight = weight;
        this.distanceCode = distanceCode;
    }

从那里,我会避免使用这些set方法。相反,当您从System.in. 获得所有字段后,创建Delivery对象。

于 2013-06-23T04:01:02.697 回答
0

或者我们可以像交付类一样简单地做到这一点

public class Delivery {
private int year,deliveryNumber,distanceCode;
private double weight;
private double fees=0;

//delivery class constructor
public Delivery(int year,int deliveryNumber,int distanceCode,double weight)
{
    this.year=year;
    this.deliveryNumber=deliveryNumber;
    this.distanceCode=distanceCode;
    this.weight=weight;
}    
//calculate  delivery fee
public void displayFees(int distanceCode, double weight)
{
    if(distanceCode == 1)
        {
        if(weight<5)
        {
            fees = 12;
        }
        else if((weight < 20)&&(weight >=5))
        {
            fees = 16.50;
        }
        else if(weight > 20)
        {
            fees = 22;
        }
    }
    else if(distanceCode == 2)
    {
        if(weight <5)
        {
            fees = 35;
        }
        else if(weight >= 5)
        {
            fees = 47.95;
        }
    }
}
public double getFee()
{
    return fees;
}

}

和 CreateDelivery 类:

public class CreateDelivery {

public static void main(String[] args) {
            

    int year=(int)ReadValues.readValue("Year", 1999,2026);
    int deliveryNumber=(int)ReadValues.readValue("Delivery Number (1 to 10000)", 0,10000);
    int distanceCode=(int)ReadValues.readValue("DistanceCode (1 or 2)",0, 3);
    double weight=ReadValues.readValue("weight",0, 20);

    Delivery delivery=new Delivery(year, deliveryNumber, distanceCode, weight);
    delivery.displayFees(distanceCode, weight);
    double fee=delivery.getFee();

    System.out.println("\n\n*****Delivery Fees Details*****\n\nTrackingNumber:"+year+deliveryNumber+"\nDistanceCode:"+distanceCode+"\nFee :"+fee);
}

}

为了从用户读取值,另一个名为 ReadValue 的类

import java.util.Scanner;

公共类 ReadValues {

public static double readValue(String prompt, int min, int max) {
    Scanner scan = new Scanner(System.in);
    double value;
    System.out.print(prompt + " :");
    while (true) {
        value = scan.nextDouble();
        if (value < min || value > max)
            System.out.println("Enter value between " + min + " & " + max);
        else
            break;
    }
    return value;
}

}

于 2021-02-16T18:44:39.990 回答