我需要從用戶那裏獲取數字的開始和結束範圍,並制定一種方法,將數字範圍返回給用戶。 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 ;
}
1.如果你正在使用int爲什麼你使用'byte'? 2.不要用'System.out.println'打包調用'numberPrinter'只需調用'numberPrinter'並設置所有的打印。 – alfasin
如果您的目標確實是要創建一個**返回**數字範圍的方法,那麼您的方法完全錯誤。你的方法當前返回一個字節,但它打印**一個數字範圍。 –
該數字必須低於100,所以我使用了字節,因爲它只允許+127的數字。好吧,我已經刪除了System.out.println,但爲什麼會停止打印兩次的最終範圍? – Ninja2k