Ask the user for an integer then ask the user if he/she wants to count up or down. Display a table of numbers where the first column contains the counter, the second column contains the counter plus 10, and the third column contains the counter plus 100. Make it so each number takes up 5 spaces total. If counting up, the first column should contain numbers 1 through the user input; If counting down, the first column should contain numbers -1 through the the negative of the user input; Do user input validation on the word "up" and "down". Allow for any case.
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
String answer = "";
while (answer.compareToIgnoreCase("up") != 0 && answer.compareToIgnoreCase("down") != 0) {
System.out.print("Do you want to count 'up' or 'down'?: ");
answer = keyBoard.nextLine();
}
if (answer.compareToIgnoreCase("up") == 0) {
for (int i = 1; i <= 5; i++) {
System.out.printf("%-5d%-5d%-5d\n", i, (i + 10), (i + 100));
}
} else {
for (int i = -1; i >= -5; i--) {
System.out.printf("%-5d%-5d%-5d\n", i, (i + 10), (i + 100));
}
}
keyBoard.close();
}
}
Comments
Leave a comment