以下代码适用于我正在参加的java课程的实验室,该课程刚刚开始了有关参数和对象的章节。我已经遇到了一个我无法过去的重大障碍。
我遇到的问题是我需要将方法GetAmountOfDestinations中的destinationAmount输入分配给方法GetItinerary,以便我可以在for循环中使用它。除了调用其他方法之外,我不能在main方法中包含任何代码。我无法改变方法的性质,因为说明书必须以某种方式制作。
我如何获得其他方法的输入?我是否创建了第三种方法来路由值?我在这个网站和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 );
}
}
}
答案 0 :(得分:0)
您需要在GetAmountOfDestinations上添加一个返回值,然后在尝试获取amountOfDestinations时调用该方法:
public static int GetAmountOfDestinations()
{
System.out.println( "Please enter the amount of destinations." );
int destinationAmount = CONSOLE.nextInt();
System.out.println( "You entered " + destinationAmount + "." );
return destinationAmount;
}
在GetItinerary中,您可以指定值:
int amountOfDestinations = GetAmountOfDestinations();
答案 1 :(得分:0)
首先,请注意GetAmountOfDestinations()
声明返回int
。您需要向此方法添加return
语句才能进行编译。
其次,您需要将返回的值分配给变量。有两种不同的方法可以做到这一点:
将返回的值分配给main()
中的变量,然后将其传递给GetItinerary()
:
public static void main(String [] args)
{
int dest = GetAmountOfDestinations();
GetItinerary(dest);
}
这要求您更改GetItinerary()
以接受int
参数,而不是拥有本地变量:
public static void GetItinerary(int amountOfDestinations)
现在删除局部变量声明。
另一种解决方案是直接从GetAmountOfDestinations()
致电GetItinerary()
:
int amountOfDestinations = GetAmountOfDestinations();
然后从GetAmountOfDestinations()
。
main()
的来电
醇>
答案 2 :(得分:0)
您需要阅读有关方法返回值和参数的更多信息,以了解下面的代码。但您需要做的就是使用从一个方法返回的值,并将其作为参数传递给下一个方法,以实现您要执行的操作。
import java.util.*;
public class TripPlanner
{
public static final Scanner CONSOLE = new Scanner ( System.in );
public static void main(String [] args)
{
int amtDestinations = GetAmountOfDestinations();
GetItinerary(amtDestinations);
}
public static int GetAmountOfDestinations()
{
System.out.println( "Please enter the amount of destinations." );
int destinationAmount = CONSOLE.nextInt();
System.out.println( "You entered " + destinationAmount + "." );
return destinationAmount
}
public static void GetItinerary(int amountOfDestinations)
{
System.out.println( "Your starting airport is SAT." );
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 );
}
}
}