java—需要一个使用方法的解决方案吗

xwmevbvl  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(258)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

5年前关门了。
改进这个问题
公共级热水器{

//public fields set to private
private double water = 1;
private double kilowatts= 2;
private double joules = 3600;
private double temp = 70;
private double jkg = 4200;
private double energy;
private double time;

//Constructor method
public WaterHeater (double water, double kilowatts, double joules, double temp, double jkg)  {
}

//Accessors and mutators
//Accessor for Water
public double getWater() {
    return water;
}
public void setWater(int water) {
    this.water = water;
}
//Accessor for Kilowatts
public double getKilowatts() {
    return kilowatts;
}
public void setKilowatts(int kilowatts) {
    this.kilowatts = kilowatts;
}
//Accessor for Temperature
public double getTemp() {
    return temp;
}
public void setTemp(int temp) {
    this.temp = temp;
}
//Method for Energy used
public double getEnergy() {
    energy = water*jkg*temp;
    return energy;
}
public void setEnergy() {
    this.energy = energy;
}
//Method for Time to boil
public double getTime() {
    time = energy/kilowatts;
    return time;
}
public void setTime() {
    this.time = time;
}

}
公营水壶热水器{

public Kettle(double water, double kilowatts, double joules, double temp, double jkg) {
    super(water, kilowatts, joules, temp, jkg);      
}

public static void main( String args[] ) 
{ 

    userInput kettleinput = new userInput();

    System.out.println("\nEnergy used: ");
    System.out.println("Time to boil: ");

}

}
公共类用户输入{

public static void main(String args[])
{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    double getWater;

    // These must be initialised
    String strWater = null;
    int    intWater = 0;

    System.out.print("Enter amount of water used: ");
    System.out.flush();

    // read string value from keyboard
    try {
        strWater = in.readLine();
    } 
    catch (IOException ioe) { 
        // ignore exception
    }

    // convert it to integer
    try {
        intWater = Integer.parseInt(strWater);
    } 
    catch (NumberFormatException nfe) {
        System.out.println("Whoops: " + nfe.toString());
        System.exit(1);
    }

    double getKilowatts;

    // These must be initialised
    String strKilowatts = null;
    int    intKilowatts = 0;

    System.out.print("Enter amount of Kilowatts used: ");
    System.out.flush();

    // read string value from keyboard
    try {
        strKilowatts = in.readLine();
    } 
    catch (IOException ioe) { 
        // ignore exception
    }

    // convert it to integer
    try {
        intKilowatts = Integer.parseInt(strKilowatts);
    } 
    catch (NumberFormatException nfe) {
        System.out.println("Whoops: " + nfe.toString());
        System.exit(1);
    }

    double getTemp;

    // These must be initialised
    String strTemp = null;
    int    intTemp = 0;

    System.out.print("Enter the temperature of water raised by: ");
    System.out.flush();

    // read string value from keyboard
    try {
        strTemp = in.readLine();
    } 
    catch (IOException ioe) { 
        // ignore exception
    }

    // convert it to integer
    try {
        intTemp = Integer.parseInt(strTemp);
    } 
    catch (NumberFormatException nfe) {
        System.out.println("Whoops: " + nfe.toString());
        System.exit(1);
    }

}

}
抱歉,代码太长了。我在寻找显示用户输入结果的解决方案时遇到问题。我有在热水器中创建的方法,我想用它们来计算用户入水时消耗的能量和沸腾的时间,千瓦和温度。这些方法已经完成了,我只是找不到一个方法来使用它们。因此,当水壶类运行时,用户输入水、千瓦和温度,它将给出一个结果。任何帮助都是值得的。

cetgtptt

cetgtptt1#

首先,你需要更好地解释一下自己。我真的不明白你真正需要什么,但这是我的尝试。
热水器
您没有在自定义构造函数中设置对象的值。
如果有些值是常量,就照原样处理( private static final ).
时间、能量等值不需要是字段,因为每次用户获取它们时都会计算它们。
水壶和用户输入
两者都有一个静态函数 main . 那是违法的。我建议您将后一个函数中的所有代码移到第一个函数中。
水壶的主要功能的代码没有意义。那根本无法编译。
userinput是一个类,所以称它为userinput(保持一致)。
请深呼吸,集中注意力,更好地解释你需要什么和你已经拥有什么。总是试着展示一个至少可以编译的代码。

vltsax25

vltsax252#

我将更改以下内容:将代码从userinput main方法移到构造函数中。然后你需要用到的所有变量,比如intwater和intkwarts,我将成为成员变量。然后我将提供公共访问器方法。
然后您需要示例化一个新的cattle并传递来自用户输入类的值。然后您就可以从cattle类获得所需的值,该类继承自该water heater类,并提供输出所需的方法。

相关问题