2016-12-13 51 views
1

我需要從用戶那裏獲取數字的開始和結束範圍,並制定一種方法,將數字範圍返回給用戶。 1, 2, 3, 4創建一個返回數字範圍的方法?

我的方法返回值包括最​​終值兩次,我想因爲我有我的返回類型在這裏設置。有沒有辦法改變我的方法,使其更好地工作?

class Test 

{ 


    public static void main (String[] args) 

    { 

     //Setup a Scanner 
     Scanner scan = new Scanner (System.in) ; 

     //Declare Variables 
     byte startNum = 0 ; 
     byte endNum = 0 ; 

     //Get User Input// 
     System.out.println (" Please enter the starting range: ") ; 
     startNum = scan.nextByte() ; 

     System.out.println (" Please enter the final range: ") ; 
     endNum = scan.nextByte() ; 

     //Call method 
     System.out.println (numberPrinter (startNum, endNum)) ; 


    } 

    // A method to print out all the numbers between startNum and endNum. 
    static byte numberPrinter (byte a, byte b) 

     { 
      byte range = 0 ; 

      while (a <= b) 

       { 
        range = a ; 
        System.out.println (range) ; 
        a++ ; 

       } 

      return range ; 

     } 
+0

1.如果你正在使用int爲什麼你使用'byte'? 2.不要用'System.out.println'打包調用'numberPrinter'只需調用'numberPrinter'並設置所有的打印。 – alfasin

+0

如果您的目標確實是要創建一個**返回**數字範圍的方法,那麼您的方法完全錯誤。你的方法當前返回一個字節,但它打印**一個數字範圍。 –

+0

該數字必須低於100,所以我使用了字節,因爲它只允許+127的數字。好吧,我已經刪除了System.out.println,但爲什麼會停止打印兩次的最終範圍? – Ninja2k

回答

3

方法numberPrinter打印所有從startNum數endNum所以沒有在打印方法的返回值點。

只需從行刪除打印:

System.out.println (numberPrinter (startNum, endNum)) ; 

到:

numberPrinter (startNum, endNum) 
+0

這有效,但爲什麼!不應該由於返回範圍而打印兩次最終值;我的方法也是我的邏輯好,我想從一開始就嘗試和學習良好的習慣。 – Ninja2k

+1

僅當您使用println方法時,該值纔會打印到屏幕上。當我們在主函數的最後一行中刪除調用時,程序打印值的唯一位置就在numberPrint方法內。這種方法打印每個數字一次 – Amityo

+0

您應該使用整數而不是字節,改變方法@Wasi艾哈邁德使用如用戶想將進入射程只有1-100 INT不浪費內存建議 – Amityo

0

你打印的numberPrinter()的返回值,這是最後一個值。從main()刪除最後一個print語句,並更改返回類型void

public static void main (String[] args) 

{ 

    //... 

    //Call method 
    numberPrinter (startNum, endNum) ; 


} 

// A method to print out all the numbers between startNum and endNum. 
static void numberPrinter (byte a, byte b) 

    { 

     while (a <= b) 

      { 
       System.out.println (a++) ; 

      } 

    } 
+0

我沒有意識到你可以使用void來接受兩個字節參數的方法。將盡快嘗試。 – Ninja2k

0

要求用戶輸入一個數a和b,並使用while,顯示你的 a++直到a < b

1

更改numberPrinter()函數的返回類型爲void,並避免在main()函數中打印來自numberPrinter()的返回值。只需撥打numberPrinter()功能即可。需要注意的是,你不需要變量range裏面的numberPrinter()函數,你可以直接使用變量a

public static void main(String[] args) { 
    // your code goes here 

    //Call method 
    numberPrinter(startNum, endNum); 
} 

// A method to print out all the numbers between startNum and endNum. 
static void numberPrinter(byte a, byte b) { 
    while (a <= b) { 
     System.out.println(a); 
     a++; 
    } 
} 

如果你有興趣在給定範圍內返回所有號碼從numberPrinter()main(),然後打印,你可以做到以下幾點。

public static void main(String[] args) { 
    // your code goes here 

    //Call method 
    byte[] result = numberPrinter(startNum, endNum); 
    for (byte value : result) { 
     System.out.println(value); 
    } 
} 

// A method to return all the numbers between startNum and endNum. 
static byte[] numberPrinter(byte a, byte b) { 
    byte[] range = new byte[b - a + 1]; 
    for (byte i = a; i <= b; i++) { 
     range[i - a] = i; 
    } 
    return range; 
} 
+0

好的,我會保存這段代碼,我需要使用我自己的代碼,因爲這是我的第一次嘗試,但將保存爲最佳做法。 – Ninja2k

+0

@ Ninja2k我很欣賞,如果您有興趣從'numberPrinter()'方法返回值,您可以在答案中看到我的更新。 –

相關問題