2017-07-03 104 views
0

以下代码是针对我刚刚开始参数和对象一章的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); 
    } 
    } 
} 
+0

问题的答案是在你的问题本身。它是关于参数和对象的一章。您可以将参数传递给方法。 public static void GetItinerary(int amountOfDestinations)然后在方法中使用它 – digidude

+0

坚持Java命名约定!方法必须以小写字母开头。 –

回答

0

您需要在您的GetAmountOfDestinations加一个return然后调用,当你试图得到的,更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(); 
+0

当我这样做时,我重复了GetAmountOfDestinations()方法。它从那里运行良好,但由于某种原因它确实重复。 –

0

首先,请注意GetAmountOfDestinations()被声明为返回int。您需要为此方法添加return语句才能编译。

其次,您将需要将返回的值分配给一个变量。有两种不同的方式来做到这一点:

  1. 分配返回值给一个变量main(),然后把它传递给GetItinerary()

    public static void main(String [] args) 
    { 
        int dest = GetAmountOfDestinations(); 
        GetItinerary(dest); 
    } 
    

    这需要你改变GetItinerary()接受的int参数,而不是有局部变量:

    public static void GetItinerary(int amountOfDestinations) 
    

    现在删除本地变量声明。

  2. 另一种解决方案是直接从GetItinerary()调用GetAmountOfDestinations()

    INT amountOfDestinations = GetAmountOfDestinations();

    然后从main()删除呼叫GetAmountOfDestinations()

+0

我可能不清楚。实验室指示说,除了调用其他方法之外,主方法中不能有其他代码。所以No.1是一个不行。至于No.2,必须从主要方法调用该方法。它不能从GetItinerar调用。 –

+0

@DariusB#1 **确实**只有调用其他方法的代码。当您了解传递参数和返回值时,这些是您拥有的唯一两个选项。如果你必须从main()调用'GetItinerary()',那么第一个或者一些细微的变化就是正确的方法。 –

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

您的代码有编译器错误。 –

+0

@ Code-Apprentice它基本上是问题中的代码。我做出的唯一改变是表明他如何根据自己的需要修改它。我自己并没有真正编译代码。我尝试将代码格式化为粗体以指示更改点,但它仅显示为星号,因为它是代码块。 – digidude

+0

这是我的观点。问题中的代码有几个错误。你的回答并不能解决与问题直接相关的问题。 –

相关问题