✔ 最佳答案
try following code:
public class ChkgetOddNum
{
public static void main(String[] args)
{ System.out.println( getOddNum( ) );
}
public static int getOddNum( )
{ java.io.DataInputStream in = new java.io.DataInputStream(System.in);
int iEnter = 0;
for ( ; ; )
{ System.out.print( "Enter a positive odd number : " );
try
{ iEnter = Integer.parseInt( in.readLine() );
if ( iEnter % 2 != 1 )
System.out.println( "Enter number is not a odd number" );
else
if ( iEnter < 0 )
System.out.println( "Enter number is no a positive number" );
else
break;
}
catch ( Exception e )
{ e.printStackTrace();
}
}
return iEnter;
}
}
Assumption:
User cannot enter the number which is over the limit value of integer data type.