2017-07-03 109 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

這是我的觀點。問題中的代碼有幾個錯誤。你的回答並不能解決與問題直接相關的問題。 –

相關問題