gpt4 book ai didi

java - 调用添加输入的方法

转载 作者:行者123 更新时间:2023-12-02 04:52:16 29 4
gpt4 key购买 nike

我在找出如何根据主类的用户输入获取输出时遇到问题。我已经有了键盘输入,用户可以输入一个值,该值将被保留。我想我需要使用它,例如(input.input1());。但是我还需要包括计算结果的方法,例如 CalculatedRocketFlightProfile 类中的 calculations.theAverageMassFfTheVehicle ,我只是不确定如何将两者结合起来以获得结果。

//计算类

public class CalculatingRocketFlightProfile { //Calculation class

//Declaring fields
public double totalImpulse ;
public double averageImpulse;
public double timeEjectionChargeFires;
public double massEmptyVehicle;
public double engineMass;
public double fuelMass;
//Declaring variables for outputs
public double theAverageMassOfTheVehicle;
public double theVehiclesMaximumVelocity;

public CalculatingRocketFlightProfile(double totalImpulse, double averageImpulse, double timeEjectionChargeFires, double massEmptyVehicle,
double engineMass, double fuelMass) { //Constructor for this class

this.totalImpulse = totalImpulse;
this.averageImpulse = averageImpulse;
this.timeEjectionChargeFires = timeEjectionChargeFires;
this.massEmptyVehicle = massEmptyVehicle;
this.engineMass = engineMass;
this.fuelMass = fuelMass;
}


//Mutators and Accessors

//Accessors
//Methods for calculations - Calculating outputs, using inputs.

public double theAverageMassOfTheVehicle() {
return massEmptyVehicle + ((engineMass + (engineMass - fuelMass) )/ 2); //Formula to calculate Average mass
}//method

public double theVehiclesMaximumVelocity() { //Formula to calculate Maximum velocity
return totalImpulse / getTheAverageMassOfTheVehicle();
}//method



//Returns - GET
public double getTheAverageMassOfTheVehicle() {
return theAverageMassOfTheVehicle;
}//method

public double getTheVehiclesMaximumVelocity() {
return theVehiclesMaximumVelocity;
}//method

}//class

//主类

public class Main { //Master class


public static void main( String args[] ) //Standard header for main method
{



kbentry input = new kbentry();

System.out.print("\nPlease enter a number for Total Impulse: " );
System.out.println("You have entered : " +input.input1());


System.out.print("\nPlease enter a number for Average Impulse: " );
System.out.println("You have entered : " +input.input2());

System.out.print("\nPlease enter a number for Time ejection charge fires: " );
System.out.println("You have entered : " +input.input3());

System.out.print("\nPlease enter a number for the Mass of the vehicle: " );
System.out.println("You have entered : " +input.input4());

System.out.print("\nPlease enter a number for the Mass of the engine: " );
System.out.println("You have entered : " +input.input5());

System.out.print("\nPlease enter a number for the Mass of the fuel: " );
System.out.println("You have entered : " +input.input6());

//Output

CalculatingRocketFlightProfile calculations = new CalculatingRocketFlightProfile();


System.out.println("\nThe average mass of the vehicle: " +calculations.theAverageMassOfTheVehicle() +
"\nThe vehicles maximum velocity: " + calculations.theVehiclesMaximumVelocity());

}
}

//kbentry

public class kbentry{

double input1(){

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

//Total Impulse entry
String strTotalImpulse = null; // These must be initialised
int intTotalImpulse = 0;

//System.out.print("Please enter a number for Total Impulse: ");
//System.out.flush();

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

// convert it to integer
try {
intTotalImpulse = Integer.parseInt(strTotalImpulse);
}
catch (NumberFormatException nfe) {
System.out.println("Error! Please enter a number!" + nfe.toString());

}

最佳答案

问题是您的 CalcultingRocketFlightProfile 类需要参数,但您在创建计算时没有将任何参数传递给新的 CalcultingRocketFlightProfile。

您应该将这些输入存储在变量中,然后将这些变量传递给您声明的新 CalcultingRocketFlightProfile 中的构造函数。

关于java - 调用添加输入的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29107794/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com