我在查找如何根据Main类的用户输入获取输出时遇到问题。我已经有键盘输入,用户可以在其中输入一个值,该值将被保留。我猜我需要使用它,例如的(input.input1()); 即可。但是我还需要在CalculatingRocketFlightProfile类中包含计算结果的方法,例如 calculate.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());
}
答案 0 :(得分:2)
问题在于您需要CalcultingRocketFlightProfile类需要参数,但是您在不将任何参数传递给新的CalcultingRocketFlightProfile的情况下创建计算。
您应该将这些输入存储在变量中,然后将这些变量传递给您声明的新CalcultingRocketFlightProfile中的构造函数。
答案 1 :(得分:0)
好吧,首先,您实际上并没有将任何输入传递给Calculations类。我不确定input.input1()是什么,或者你有一个你没有发布的输入类。无论哪种方式,您都可以通过几种不同的方式来实现
首先,请为输入变量指定一个有意义的名称,以便了解您正在处理哪些变量。然后传递你的所有输入。
CalculatingRocketFlightProfile calculation = new CalculatingRocketFlightProfile(input1,input2等)
或
将所有输入变量放入计算类中。然后将用户输入存储为calculate.totalImpulse等...然后调用计算方法显示答案。
- 编辑 -
只有2个课程,主要课程和计算课程。不需要另一个类来处理键盘输入。
实施例
主要课程
public class Main {
public static void main( String args[] ) {
Scanner keyboard = new Scanner(System.in);
CalculatingRocketFlightProfile calculations = new CalculatingRocketFlightProfile();
System.out.print("\nPlease enter a number for Total Impulse: " );
calculations.totalImpulse = keyboard.nextDouble();
System.out.println("You have entered : " + calculations.totalImpulse);
}
}
public class CalculatingRocketFlightProfile { //Calculation class
//Declaring fields
public double totalImpulse ;
// Do all of your maths, and methods for answer return
}
您实际上并未使用键盘输入并将其分配给任何内容。使用扫描仪对象,您可以将输入分配给计算类中的变量。如果你为所有这些做到这一点,你实际上并不需要计算类中的构造函数,只需用它来完成所有数学运算并返回答案。