我需要从用户那里获取数字的开始和结束范围,并制定一种方法,将数字范围返回给用户。 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