gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:19 25 4
gpt4 key购买 nike

我有两个类,一个只有一个 main(String args[]) 方法,另一个则包含所有其余方法。我试图将主方法中的数据获取到第二个类中来运行它,然后将信息返回给主方法。我的程序正在编译,但每次我输入新数字时数字应该会下降,但事实并非如此。任何帮助表示赞赏!

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

}
}

最佳答案

您的方法变量 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;
}

}

关于java - 将数据从主方法导入到另一个方法中,然后在再次运行程序之前存储该值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25023558/

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