Java program pls help me !!

2007-07-14 5:03 pm
Write a method getOddNum() to get a positive odd number If the user enters an invalid input (not positive odd number),appropriate message will be display before prompting the user enter again .

回答 (1)

2007-07-14 9:05 pm
✔ 最佳答案
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.


收錄日期: 2021-04-23 17:08:58
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070714000051KK00648

檢視 Wayback Machine 備份