【问题标题】:Struggles in using multiple classes努力使用多个类
【发布时间】:2022-01-05 09:06:28
【问题描述】:

所以目前,我正在努力在 eclipse 中制作一个特定的程序来完成一项任务,虽然我能够制作大部分程序,但我似乎在为程序的无参数部分以及带来碎片而苦苦挣扎第一堂课暂时进入第二堂课。这是我的头等舱代码

// Preparation of the input
import java.util.Scanner;

public class primarySetUp {

public static void main(String[] args) {
    // Variable Declaration
    double userBagNumber;
    double userBagWeight;
    
    // Create Scanner
    Scanner input = new Scanner(System.in);
    java.util.Date date = new java.util.Date();
    
    // Opening Statement
    System.out.println("Welcome to the Coffee Sales Simulation!");
    
    // Get User Input
    System.out.println("How heavy do you want the bags to be?");
    userBagWeight = input.nextDouble();
    System.out.println("How many bags do you want?");
    userBagNumber = input.nextDouble();
    
    // Get output
    // Date
    System.out.println("Todays date:  ");
    System.out.printf("%tB %<te, %<tY", date);
    System.out.println(""); // spacer
    // Original Inputs
    System.out.printf("\nNumber of Bags: %3.0f", userBagNumber);
    System.out.printf("\nWeight of Each Bag: %3.2f", userBagWeight);
    System.out.print(" lbs");
    
    // Calling of the Class
    secondarySetUp mysecondarySetUp = new secondarySetUp(userBagWeight, userBagNumber);
    
    // End Program
    System.out.println("\nThank you for shopping with us!");
}

}

这是我的第二类代码,在这种情况下它充满了错误。

public class secondarySetUp {
    
    // Constants
    static double pricePerPound = 5.99;
    static double taxRate = 0.0725;
    
    int singleBagger, pounderBagger;
    public secondarySetUp(double userBagWeight, double userBagNumber) {
    
        
        
        
    //  A method named getTaxRate() that returns the tax rate.  
        
        System.out.printf("\nPrice per Pound: $%2.2f", getPrice());
        System.out.printf("\nSales Tax: $%2.2f", getTaxRate());
        System.out.print(" %");
        System.out.printf("\nPrice of one bag weighing one pound: %3.2f", getSale());
        
    }
    // No argument pricing
    public Sale() {
            singleBagger = 1;
            pounderBagger = 1;
        }
    // First constructor receiving No argument pricing
    public Sale(int w, int n) {
            singleBagger = w;
            pounderBagger = n;
        }
    // Sale without tax
    public double getSale() {
        return userBagWeight * singleBagger * pounderBagger;
    }
    // Get Sale Tax
    public double getSaleTax() {
        return (getSale() * taxRate);
    }
    // Get total pricing
    public double getTotalPrice() {
        return (getSale() + getSaleTax());
    }
    public double getPrice() {
        return pricePerPound;
    }
    public double getTaxRate() {
        return taxRate * 100;
    }
}

如果您有任何我可以应用的修复方法,请告诉我;我还计划为其余参数添加打印语句,但我想先修复 Sale()。

【问题讨论】:

    标签: java eclipse class


    【解决方案1】:

    我在getSale() 中看到一个问题,您尝试使用userBagWeight,但该变量在构造函数参数之外不存在,这可能会产生很多问题,因为其他methods 正在调用它。构造函数采取 double userBagWeight, double userBagNumber,但它没有将它们分配给任何字段或对它们做任何事情。

    我错过了您将Sale() 视为constructor 的部分,但那些不是constructorsconstructor 以你的类名命名。

    public secondarySetUp(double userBagWeight, double userBagNumber)
    

    Sale() 更改为secondarySetUp,你会没事的。

    你的班级应该是这样的:

    public class secondarySetUp {
    
    // Constants
    static double pricePerPound = 5.99;
    static double taxRate = 0.0725;
    
    int singleBagger, pounderBagger;
    double userBagWeight, userBagNumber;
    public secondarySetUp(double userBagWeight, double userBagNumber) {
        this.userBagWeight = userBagWeight;
        this.userBagNumber = userBagNumber;
    
        singleBagger = 1;
        pounderBagger = 1;
        
        
        
    //  A method named getTaxRate() that returns the tax rate.  
        
        System.out.printf("\nPrice per Pound: $%2.2f", getPrice());
        System.out.printf("\nSales Tax: $%2.2f", getTaxRate());
        System.out.print(" %");
        System.out.printf("\nPrice of one bag weighing one pound: %3.2f", getSale());
        
    }
    // First constructor receiving No argument pricing
    public secondarySetUp(int w, int n) {
            singleBagger = w;
            pounderBagger = n;
        }
    // Sale without tax
    public double getSale() {
        return userBagWeight * singleBagger * pounderBagger;
    }
    // Get Sale Tax
    public double getSaleTax() {
        return (getSale() * taxRate);
    }
    // Get total pricing
    public double getTotalPrice() {
        return (getSale() + getSaleTax());
    }
    public double getPrice() {
        return pricePerPound;
    }
    public double getTaxRate() {
        return taxRate * 100;
    }
    

    }

    this 是一个关键字,告诉程序我们要使用字段“实例变量”,如果我们有一个带有 parameter 的方法与字段名称相同,那么为了区分它们,我们告诉程序this.fieldName 知道我们在说哪一个。

    【讨论】:

    • 那么,在这种情况下我该怎么办?我确实尝试将它们转移到包含这些的类下,但这会导致错误。我可以添加或改变什么以使其更可行?
    • 如果你想在构造函数之外的其他方法中使用它们,你需要为它们分配字段。在你的常量下面为它们创建一个字段double userBagWeight; 并在构造函数中你做这样的事情:public secondarySetUp(double userBagWeight, double userBagNumber) { this.userBagWeight = userBagWeight;
    • 它只是给我一个错误,指出“userBagWeight 无法解析或不是一个字段”,Sale() 也不想运行,它指出返回类型方法是失踪。我可以为这些错误做些什么?
    • 检查答案中的编辑
    • 我仍在努力寻找为什么我的错误会出现这样的情况,我仍然没有找到我应该把这个语句放在哪里,这是我的新 Sale() 代码' public double getSale(double userBagWeight) { return userBagWeight * singleBagger * pounderBagger; } '出现这样的错误“secondarySetUp类型中的方法getSale(double)不适用于参数()”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2015-02-16
    • 2020-11-30
    相关资源
    最近更新 更多