Popular MMO (massively multiplayer online) Games use \"slash commands\" in the i
ID: 3802508 • Letter: P
Question
Popular MMO (massively multiplayer online) Games use "slash commands" in the in-game chat interface to perform various tasks. These commands range from making your character "emote" (dance, laugh, etc) or roll random numbers, or invite people to groups and so on. Implement a simple chat interface that accepts slash commands and displays the required output. A slash command starts with a forward slash followed b the command i.e./dance. Ask the user to enter their character name then display a chat prompt which is just the greater than sign >. The command will be entered after this prompt. Implement the following commands. bullet/roll This should generate and display a random number between [0, 100]. It should display the message rolled a #. bullet/dance Display the message " performs a lively dance." bullet/invite Display the message " has been invited to 's party." bullet/say Display the message " says " Input Validation: bullet If the command entered is not a valid command, display an error message and exit the program. Requirements: bullet Assume character names will only be one word. bullet The commands must be entered all on one line. Sample Output 1: Enter your character name: Java_L337 >/roll > Java_L337 rolled a 75.Explanation / Answer
CommandsTEst.java
import java.util.Random;
import java.util.Scanner;
public class CommandsTEst {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter your chracter name: ");
String s = scan.nextLine();
System.out.print(">");
String command = scan.next();
if(command.equalsIgnoreCase("/roll")){
Random r = new Random();
int randNum = r.nextInt(101);
System.out.println("> "+s+" rolled a "+randNum);
}
else if(command.equalsIgnoreCase("/dance")) {
System.out.println(s+" performs a lively dance.");
}
else if(command.equalsIgnoreCase("/invite")) {
String personToInvite = scan.nextLine();
System.out.println(personToInvite+" has been invited to "+s+"'s party.");
}
else{
String message = scan.nextLine();
System.out.println(s+" says "+message);
}
}
}
OUtput:
Enter your chracter name: Suresh
>/say how are you
Suresh says how are you