2016-09-25 39 views
0

我一直在搜尋這個網站和其他人在過去的兩天,因此我意識到已經存在的問題與此類似的壓倒性數額。但是,在三四周前纔開始編程,我無法理解它們中的任何一個及其線程。考慮到這一點,我有一個問題,對更有經驗的人可能擁有一個痛苦的基本和直接的解決方案。代碼無法從方法返回整數值(Java/Eclipse)


使用Eclipse,我必須創建一個程序,它執行以下操作:

  1. 讀取在兩個數字(整數)
  2. 包含,是以兩個整數作爲參數並返回大的方法的兩個。
  3. 包含從main()調用的另一個方法將(2)中方法返回的較大數字作爲參數並返回最後一位數字。
  4. 打印(2)和(3)中的方法返回的兩個值。

我已經創建的代碼(下面顯示)成功讀取的用戶輸入,但無法打印出兩個較大的整數和大整數的最後一位數字。它會打印出「0」。這就是打印到控制檯:


Enter First Integer: (Entered Integer) 
Enter Second Integer: (Entered Integer) 
Larger Integer: 0 
Last Digit: 0 

我相信,代碼應該正常運行,保存該值的方法內確定沒有被返回到主的事實。 Eclipse沒有顯示錯誤消息,這導致我相信問題出在我的返回語句中。任何建議或解決方案都是歡迎和期望的。謝謝。


import java.util.Scanner; 

public class thursdayWork{ 

//Determines larger integer and returns it. 

    public static int processAndReturn(int int1, int int2, int answer){ 

     answer = Math.max(int1, int2); 
     return answer; 

    } 

//Determines last digit of integer and returns it. 

    public static int processLargerInt(int answer, int lastDigit){ 

     if((answer >= 0) && (answer < 10))lastDigit = (answer % 10); 
      else if((answer >= 10) && (answer < 100))lastDigit = (answer % 100); 
      else if((answer >= 100) && (answer < 1000))lastDigit = (answer % 1000); 
       else if((answer >= 1000) && (answer < 10000))lastDigit = (answer % 10000); 
       else System.out.print("Pick smaller numbers."); 
return lastDigit; 

    } 

//Calls methods and prints returned values. 

    public static void main(String[] args){ 

     Scanner console = new Scanner(System.in); 
     int int1; 
     int int2; 
     int answer = 0; 
     int lastDigit = 0; 

     System.out.print("Enter First Integer: "); 
      int1 = console.nextInt(); 
     System.out.print("Enter Second Integer: "); 
      int2 = console.nextInt(); 
     processAndReturn(int1, int2, answer); 
     System.out.println("Larger Integer: " + answer); 
     processLargerInt(answer, lastDigit); 
     System.out.print("Last Digit: " + lastDigit); 

    } 
} 
+0

你不存儲任何地方的方法的返回值。 –

+0

使用你的代碼,把你的第一個方法調用的'answer ='infront和'lastDigit ='你的第二個方法的前面 –

回答

0

而不是將答案作爲參數傳遞給兩個方法,你應該從方法返回它。您應該詳細瞭解按值傳遞與傳遞參考。

public static void main(String[] args) { 
    // Your scanner code here. 
    int answer = processAndReturn(int1, int2); 
    System.out.println("Larger Integer: " + answer); 
    int lastDigit = processLargerInt(answer); 
    System.out.print("Last Digit: " + lastDigit); 
} 

public static int processAndReturn(int int1, int int2){ 
    return Math.max(int1, int2); 
} 

public static int processLargerInt(int answer) { 
    return answer % 10; 
} 
0

你的問題是你如何處理processAndReturn。 Java按值傳遞基元(如int)。這意味着當你給answer使用方法processAndReturn時,它實際上使用了變量的,所以你對它所做的任何更改都不存在於你調用它的地方。要解決這個問題的方法是通過使用processAndReturn 的返回值,您應該修改該行是這樣的:

int answer = processAndReturn(int1, int2); 

你還需要修改processAndReturn到只需要兩個參數。 這將是這個樣子:

static int processAndReturn(int int1, int int2){ 
    return Math.max(int1, int2); //Note: you can just replace a call to this 
    // With a call to max, meaning it's pointless 
} 

你需要做processLargerInt類似的東西爲好。

在一個不相關的說明中,這與eclipse沒有任何關係。那是你的發展環境。這與java有關,所以它不會影響你正在開發它的內容。