所以我一直在看這段代碼整個下午,我看不到錯誤(S)。這是我應該做的:Java - 調用從其他類計算字段的方法
創建一個Delivery
類爲一個送貨服務。該類包含以下字段:
- 包含八位數字的投遞號碼。前四位數字代表年份,最後四位數字代表送貨編號。
- 代表交貨區域的代碼。本地配送是代碼1,長途配送是代碼2.
- 重量(磅),要交付的物品。
- 用於遞送的費用,如下:
創建用於Delivery
類接受參數的一年一個構造函數,年,輸送距離碼,和重量的包內 交貨編號。構造函數決定了八位數的遞送編號和遞送費用。還包括顯示每個Delivery
對象字段的 方法。將該文件保存爲Delivery.java。
接下來,創建一個提示用戶輸入數據的應用程序。保持 提示以下每個值的用戶,直到它們是有效的:
- 2001年至2025年之間的四位數年份包容
- 1和9999包
- A之間的全年投放量0.10磅和100磅包容
- 的輸送距離的代碼,或者是1或2
當所有的數據項是有效的,構造一個之間包重對象,然後顯示其值 值。將該文件保存爲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'和'CreateDelivery'是否在同一個包中? – Genzer
//掃描儀的距離代碼 \t \t \t System.out.print(「請輸入1爲本地交貨,2爲長途交貨>>」); \t \t \t delivery1.setDistanceCode(input.nextInt()); \t \t \t \t \t \t \t \t //循環用於遞送代碼 \t \t \t \t \t而(((delivery1.getDistanceCode()<= 1)||(delivery1.getDistanceCode()> = 2))) \t \t \t \t \t { \t \t \t \t \t \t的System.out.println(「發生錯誤,請輸入一個有效的距離碼(1對於本地交付和2對於長途交貨)>>「); \t \t \t \t \t \t delivery1.setDistanceCode(input.nextInt()); \t \t \t \t \t} –