Answer to Question #58889 in Java | JSP | JSF for James

Question #58889
Write an application that asks a user to type an even number to continue or to type 999 to stop. When the user types an even number, display the message “Good job!” and then ask for another input. When the user types an odd number, display an error message and then ask for another input. When the user types 999, end the program.
1
Expert's answer
2016-04-05T10:19:05-0400
import java.io.DataInputStream;
import java.util.Scanner;

public class App {

static int number;
static Scanner scanner = new Scanner(new DataInputStream(System.in));

public static void main(String[] args) {
System.out.println("Hi!");
while (true) {
System.out.println("Please type an even number to continue or '999' to stop");
number = scanner.nextInt();
if (number == 999) {
System.out.println("Bye!");
System.exit(0);
}
if (number % 2 != 0) {
System.out.println("Wrong input! Try again.");
} else {
System.out.println("Good job!");
}
}
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS