1. Create, using NetBeans, a complete Java program called GameControl according
ID: 3668272 • Letter: 1
Question
1. Create, using NetBeans, a complete Java program called GameControl according to the following guidelines. This program must use a do loop to provide the following interac on with a user. The program should prompt for an integer from 1 through 4, or q to quit. If a user enters 1, your program should print "Up" and then prompt the user to enter another integer from 1 through 4, or q to quit. If a user enters 2, your program should print "Down" and then prompt the user to enter another integer from 1 through 4, or q to quit. If a user enters 3, your program should print "Le " and then prompt the user to enter another integer from 1 through 4, or q to quit. If a user enters 4, your program should print "Right" and then prompt the user to enter another integer from 1 through 4, or q to quit. If a user enters q, your program should print “Termina ng on receiving input q” and quit. Thoughts: *)
Note the requirement for this program to use a do loop (Horstmann sec on 4.4). *) Find a simple solu on that works for you.
Explanation / Answer
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char x = 0;
do{
System.out.println("Enter an integer from 1 through 4, or q to quit: ");
x = sc.next().charAt(0);// taking user input as character, as i have to check aginst 'q'
int num = x - '0'; // converting user input as an integer
switch(num){
case 1:
System.out.println("Up");
break;
case 2:
System.out.println("Down");
break;
case 3:
System.out.println("Left");
break;
case 4:
System.out.println("Right");
break;
default:
System.out.println("Invalid Input. Try Again..");
}
}while(x !='q');
System.out.println("Termina ng on receiving input q");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.