Please input a number x, if the number is even, then shows that \" The number x
ID: 3632874 • Letter: P
Question
Please input a number x, if the number is even, then shows that " The number x is even number“; if the number is odd, then shows " The number x is odd numberIf you input a number it is not integer, and please shows "Please input the right format: (It must be a integer type)" Hint: Use "try" and "catch"
If you input "1000", and then stop this program (Hint: Use “ while”)
--------------------------------------------------------------
Output:
Please input a number:(Stop this programming if you input 1000)
5
The number 5 is odd number
Please input a number:(Stop this programming if you input 1000)
6
The number 6 is even number
Please input a number:(Stop this programming if you input 1000)
5.5
Please input the right format: (It must be a integer type)
Please input a number:(Stop this program if you input 1000)
1000
Please answer with Java Code and not C++!
I won't forget to give rating. Thanks!
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaNumbers
{
public static void main(String[] args) throws IOException
{
InputStreamReader inp = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(inp);
while(true)
{
try
{
System.out.println("Please input a number:(Stop this programming if you input 1000)");
int x = Integer.parseInt(input.readLine());
if(x == 1000)
{
break;
}
else if(x%2 == 0)
{
System.out.println("The number "+x+" is even number");
}
else
{
System.out.println("The number "+x+" is odd number");
}
}
catch(Exception exp)
{
System.out.println("Please input the right format: (It must be a integer type)");
}
}
}
}