以下代码是针对我刚刚开始参数和对象一章的java课程的实验。我已经遇到了一个我无法逾越的重大的瓶颈点。将输入值从一种方法传递到另一种方法
我遇到的问题是我需要将方法GetAmountOfDestinations中的destinationAmount分配给方法GetItinerary,以便我可以在for循环中使用它。除了调用其他方法之外,我不能在主方法中使用任何代码。我不能改变方法的性质,因为说明书必须以某种方式进行。
我怎样才能得到其他方法的输入?我是否创建了第三种方法来路由该值?我在这个网站和YouTube上找了几个小时,而且我没有看到这种情况。我会很感激帮助。
import java.util.*;
public class TripPlanner
{
public static final Scanner CONSOLE = new Scanner (System.in);
public static void main(String [] args)
{
GetAmountOfDestinations();
GetItinerary();
}
public static int GetAmountOfDestinations()
{
System.out.println("Please enter the amount of destinations.");
int destinationAmount = CONSOLE.nextInt();
System.out.println("You entered " + destinationAmount + ".");
}
public static void GetItinerary()
{
System.out.println("Your starting airport is SAT.");
int amountOfDestinations = ??????;
System.out.println(amountOfDestinations);
double currentDestinationLatitude = Airports.getAirportLatitude("SAT");
double currentDestinationLongitude = Airports.getAirportLongitude("SAT");
for(int x = 0; x < amountOfDestinations; x = x + 1)
{
System.out.println("Please enter the next destination.");
String nextDestination = CONSOLE.next();
double nextDestinationLatitude = Airports.getAirportLatitude(nextDestination);
double nextDestinationLongitude = Airports.getAirportLongitude(nextDestination);
System.out.println(nextDestinationLatitude + " " + nextDestinationLongitude);
}
}
}
问题的答案是在你的问题本身。它是关于参数和对象的一章。您可以将参数传递给方法。 public static void GetItinerary(int amountOfDestinations)然后在方法中使用它 – digidude
坚持Java命名约定!方法必须以小写字母开头。 –