Java 将数据从一个主方法导入另一个方法,然后在再次运行程序之前存储该值

Java 将数据从一个主方法导入另一个方法,然后在再次运行程序之前存储该值,java,Java,我有两个类,一个只有main(stringargs[])方法,另一个包含所有其余的方法。我试图将主方法中的数据输入到第二个类中运行它,然后将信息返回给主方法。我的程序正在编译,但每次我输入新的数字时,数字都应该下降,但事实并非如此。感谢您的帮助 public class LunarLander { public static void move(double efficiency, double fuelLeft, int maxFuel, int time, double Altitude

我有两个类,一个只有
main(stringargs[])
方法,另一个包含所有其余的方法。我试图将主方法中的数据输入到第二个类中运行它,然后将信息返回给主方法。我的程序正在编译,但每次我输入新的数字时,数字都应该下降,但事实并非如此。感谢您的帮助

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;
  }

}

如果重新设计程序,使一个程序的输出成为另一个程序的输入,则可以使用管道以如下方式运行程序:

prog1.exe > prog2.exe
prog1.exe < prog2.exe
prog1.exe | prog2.exe
prog1.exe>prog2.exe
prog1.exe
您可以做的另一件事是使用网络实现客户机-服务器解决方案

您的方法可变高度是该方法的局部高度。这意味着每次调用该方法时都会重新创建并重新分配它的值。在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;
    }

}
公共类LunarLander{
双海拔;
双垂直速度;
双燃料EFT;
公共月球车(双高度、双垂直速度、双燃油左){
这个高度=高度;
此参数。垂直速度=垂直速度;
this.fueleft=fueleft;
}
公共无效移动(双效率,int maxFuel,
整数时间,双输入速率){
//改变产出的公式
双速度=时间*(效率-1.62);//给出公式
//计算速度
double altChng=time*velocity;//为atitude创建一个变量
//查格
//例外情况
如果(效率>0&&FuelEFT==0){//在以下情况下将效率更改为0
//没有燃料了
效率=0;
}否则{
}
//新产出
高度=高度-altChng;//通过减去
//高度变化
速度=时间*(效率-1.62);//给出计算公式
//速度
altChng=time*velocity;//创建一个变量用于幅度变化
垂直速度=速度;//因为船只会移动
//不来回的速度是多少
//速度
效率=inputFuelRate/maxFuel;//重新计算效率
double fueloss=time*fueleft*maxFuel;//要确定的新变量
//燃烧了多少燃料
//期间
fueleft=fueleft-fueloss;//更改剩余燃油的值
}
公共布尔值(){
如果(高度==0和垂直速度<-1){
返回true;
}否则{
返回false;
}
}
公共字符串toString(){
字符串输出=”;
输出+=“鹰:\n”;
输出+=“高度=”+高度+“\n”;
输出+=“速度=”+垂直速度+“\n”;
输出+=“Fuel=“+fueleft+”\n”;
返回输出;
}
}

请阅读您的LunarLander方法不会以您期望的方式修改变量。此外,您的设计也不好。考虑每个LunalLANER实例应该有它自己的变量(海拔、燃料、速度等)。虽然我很欣赏你的反应,这真的没有帮助。如果有些东西“不好”,举个例子会更有帮助,我是说海拔高度(以及您正在使用的一些其他变量)属于LunarLander实例
class LunarLander{double altitude;static int maxstruch=10000;//etc.}
在主菜单中,创建一个类似于
的新LunarLander(10.0,1000.0,…)或创建一个使用默认值的默认构造函数。根据我从问题中解释的,它是一个包含2个类的程序。是的,它是一个程序!非常感谢你!你建议如何修理?我应该把变量声明移到方法之外吗?不完全是这样。让我给你写代码。等一下:POK,现在检查我的程序,希望你理解我所做的。这非常有帮助!使用这个。很好,很多地方更简单。非常感谢。
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;
    }

}