我有两个类,一个只有main(String args[])
方法,另一个包含所有其他类。我试图从main方法获取数据到第二个类来运行它,然后将信息返回给main方法。我的程序正在编译,但每次输入新数字时数字应该会下降,但它们不是。任何帮助表示赞赏!
public class LunarLander
{
public static void move(double efficiency, double fuelLeft, int maxFuel, int time, double Altitude, double inputFuelRate)
{
//formulas to change outputs
double velocity = time * (efficiency - 1.62); //given formula to caluculate velocity
double altChng = time * velocity; //creates a variable for atitude chage
//exceptions
if (efficiency > 0 && fuelLeft == 0){ //changes efficiency to 0 when there is no fuel left
efficiency = 0;
}
else{
}
//new outputs
Altitude = Altitude - altChng; //calculates new altitude by subtracting altitude change
velocity = time * (efficiency - 1.62); //given formula to caluculate velocity
altChng = time * velocity; //creates a variable for atitude chage
double verticalSpeed = velocity; //since the ship would only move and not go back and forth velocity is speed
efficiency = inputFuelRate / maxFuel; //recalculates efficiency
double fuelLoss = time * fuelLeft * maxFuel;// new variable to determine how much fuel was burned during time period
fuelLeft = fuelLeft - fuelLoss; //changes the values for fuel left
}
public static boolean crashed(double Altitude, double verticalSpeed)
{
if (Altitude == 0 && verticalSpeed <-1){
return true;
}
else{
return false;
}
}
public String toString(double Altitude, double verticalSpeed, double fuelLeft){
String output = "";
output += "Eagle: \n";
output += "Altitude = " + Altitude + "\n";
output += "Speed = " + verticalSpeed + "\n";
output += "Fuel = " + fuelLeft + "\n";
return output;
}
}
主要方法:
import java.util.Scanner;
public class Pilot
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
LunarLander lander = new LunarLander(); // create a LunarLander object
double Altitude = 10.0;
String Name = "Eagle";
double fuelLeft = 1000.0;
int shipWeight = 400;
int maxThrust = 10000;
int verticalSpeed = 0;
double efficiency = 0;
int maxFuel = 400; //max fuel flow
System.out.println("Initial data: ");
System.out.println("Altitude = " + Altitude);
System.out.println("Speed = " + verticalSpeed);
System.out.println("Fuel = " + fuelLeft);
while (lander.crashed(Altitude, verticalSpeed) != true && Altitude > 0)
{
System.out.println("Please enter a time in seconds: ");
int time = kb.nextInt();
System.out.println("Please enter a fuel rate between 0 and 1");
double inputFuelRate = kb.nextDouble();
System.out.println("Input time increment: " + time);
System.out.println("Input fuel rate: " + inputFuelRate);
lander.move(efficiency, fuelLeft, maxFuel, time, Altitude, inputFuelRate);
System.out.println(lander.toString(Altitude, verticalSpeed, fuelLeft));
}
}
}
答案 0 :(得分:0)
如果您重新设计了程序,那么一个程序的输出就是另一个程序的输入,您可以使用管道像这样运行它:
prog1.exe > prog2.exe
prog1.exe < prog2.exe
prog1.exe | prog2.exe
您可以做的另一件事是使用网络的客户端服务器解决方案 http://docs.oracle.com/javase/tutorial/networking/index.html
答案 1 :(得分:0)
您的方法变量Altitude是方法的本地变量。这意味着每次调用方法时都会重新创建和重新分配值。在java中,方法的所有参数都按值传递,这意味着方法内部参数的更改在其外部不可见,基本上它在调用时会创建参数的副本。您需要通过在类中定义不在任何方法内部来使其成为实例变量。每次调用方法时,您希望更改的其他变量也是如此。
import java.util.Scanner;
public class Pilot
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
double Altitude = 10.0;
String Name = "Eagle";
double fuelLeft = 1000.0;
int shipWeight = 400;
int maxThrust = 10000;
int verticalSpeed = 0;
double efficiency = 0;
int maxFuel = 400; //max fuel flow
LunarLander lander = new LunarLander(Altitude, verticalSpeed, fuelLeft); // create a LunarLander object
System.out.println("Initial data: ");
System.out.println("Altitude = " + Altitude);
System.out.println("Speed = " + verticalSpeed);
System.out.println("Fuel = " + fuelLeft);
while (lander.crashed() != true && Altitude > 0)
{
System.out.println("Please enter a time in seconds: ");
int time = kb.nextInt();
System.out.println("Please enter a fuel rate between 0 and 1");
double inputFuelRate = kb.nextDouble();
System.out.println("Input time increment: " + time);
System.out.println("Input fuel rate: " + inputFuelRate);
lander.move(efficiency, maxFuel, time, inputFuelRate);
System.out.println(lander.toString());
}
}
}
LunarLander类
public class LunarLander {
double Altitude;
double verticalSpeed;
double fuelLeft;
public LunarLander(double Altitude, double verticalSpeed, double fuelLeft){
this.Altitude = Altitude;
this.verticalSpeed = verticalSpeed;
this.fuelLeft = fuelLeft;
}
public void move(double efficiency, int maxFuel,
int time, double inputFuelRate) {
// formulas to change outputs
double velocity = time * (efficiency - 1.62); // given formula to
// caluculate velocity
double altChng = time * velocity; // creates a variable for atitude
// chage
// exceptions
if (efficiency > 0 && fuelLeft == 0) { // changes efficiency to 0 when
// there is no fuel left
efficiency = 0;
} else {
}
// new outputs
Altitude = Altitude - altChng; // calculates new altitude by subtracting
// altitude change
velocity = time * (efficiency - 1.62); // given formula to caluculate
// velocity
altChng = time * velocity; // creates a variable for atitude chage
verticalSpeed = velocity; // since the ship would only move and
// not go back and forth velocity is
// speed
efficiency = inputFuelRate / maxFuel; // recalculates efficiency
double fuelLoss = time * fuelLeft * maxFuel;// new variable to determine
// how much fuel was burned
// during time period
fuelLeft = fuelLeft - fuelLoss; // changes the values for fuel left
}
public boolean crashed() {
if (Altitude == 0 && verticalSpeed < -1) {
return true;
} else {
return false;
}
}
public String toString() {
String output = "";
output += "Eagle: \n";
output += "Altitude = " + Altitude + "\n";
output += "Speed = " + verticalSpeed + "\n";
output += "Fuel = " + fuelLeft + "\n";
return output;
}
}