命令解释器问题

huangapple go评论82阅读模式
英文:

Trouble with command intepreter

问题

晚上好,

我正在为大学的项目做一个工作,但我发现自己有点烦恼。我在处理我的命令解释器时遇到了麻烦。我可以让代码运行,但不幸的是它不能正确地读取我的输入。有人能指点我朝着找出错误的方向吗?下面是代码,忽略注释和其他支持类的实例。非常感谢提前帮助!

import java.util.Scanner;

public class Main {
    // 命令定义的常量
    public static final String REGISTER = "register";
    public static final String LISTUSERS = "listusers";
    public static final String UPLOAD = "upload";
    public static final String READ = "read";
    public static final String WRITE = "write";
    public static final String GRANT = "grant";
    public static final String REVOKE = "revoke";
    public static final String USERDOCS = "userdocs";
    public static final String TOPLEAKED = "topleaked";
    public static final String TOPGRANTERS = "topgranters";
    public static final String HELP = "help";
    public static final String EXIT = "exit";

    // 用户消息的常量
    public static final String EXIT_COMM = "再见!";
    public static final String REGISTER_COMM = " 已注册。";
    public static final String ALREADY_REG_ERROR = " 已经分配给另一个用户。";
    public static final String NO_LIST_USERS = "没有已注册的用户。";
    public static final String DOC_UPLOADED = " 已上传。";
    public static final String OFFICIAL_DOC = "联系人更新。";
    public static final String TOPSECRET_DOC = "联系人名单为空。";
    public static final String CONFIDENTIAL_DOC = "电话号码不存在。";
    public static final String CONTACT_SHARE_PHONE = "有共享电话号码的联系人。";
    public static final String HELP_COMM = "register - 注册新用户 /n";

    public static void main(String[] args) {
        System.out.println("欢迎!");
        Scanner in = new Scanner(System.in);
        String comm = getCommand(in);

        while (!comm.equals(EXIT)) {
            switch (comm) {
                case HELP:
                    System.out.println(HELP);
                default:
                    System.out.println("错误");
                    break;
            }
            comm = getCommand(in);
        }
        System.out.println(EXIT_COMM);
        in.close();
    }

    private static String getCommand(Scanner in) {
        String input;

        input = in.nextLine().toUpperCase();
        return input;
    }
}
英文:

Evening guys,
I'm doing a project for uni and I find myself in kind of a fuss.
I'm having trouble with my command interpreter.
I can get the code to run, but unfortunately it's not reading my input right.
Could someone point me in the right direction towards approaching the error ?
The code follows below, ignore the comments and the instances of other supporting classes.
Any help would be appreciated.
Thanks in advance !

import java.util.Scanner;
public class Main {
//Constantes que definem os comandos
public static final String REGISTER     = "register";
public static final String LISTUSERS    = "listusers";
public static final String UPLOAD       = "upload";
public static final String READ         = "read";
public static final String WRITE        = "write";
public static final String GRANT        = "grant";
public static final String REVOKE       = "revoke";
public static final String USERDOCS     = "userdocs";
public static final String TOPLEAKED 	 = "topleaked";
public static final String TOPGRANTERS	 = "topgranters";
public static final String HELP 	     = "help";
public static final String EXIT         = "exit";
//Constantes que definem as mensagens para o utilizador
public static final String EXIT_COMM 			= "Bye!";
public static final String REGISTER_COMM		= " was registered.";
public static final String ALREADY_REG_ERROR 	= " is already assigned to another user.";
public static final String NO_LIST_USERS		= "There are no registered users.";
public static final String DOC_UPLOADED	 	= " was uploaded.";
public static final String OFFICIAL_DOC 		= "Contact upda1ted.";
public static final String TOPSECRET_DOC 		= "Contact book empty.";
public static final String CONFIDENTIAL_DOC 	= "Phone number does not exist.";
public static final String CONTACT_SHARE_PHONE = "There are contacts that share phone numbers.";
public static final String HELP_COMM 			= "register - registers a new user /n";
public static void main(String[] args) {   
System.out.println("Welcome!");
//WeKeepSecrets app = new WeKeepSecretsClass();		
Scanner in 	= new Scanner(System.in);
String comm = getCommand(in);
while (!comm.equals(EXIT)){
switch (comm) {
case HELP:
System.out.println(HELP);
default:
System.out.println("ERRO");
break;
}
comm = getCommand(in);
}
System.out.println(EXIT_COMM);
in.close();
}
private static String getCommand(Scanner in) {
String input;
input = in.nextLine().toUpperCase();
return input;
}
}

答案1

得分: 1

你的命令都是小写的,但你将用户输入转换为大写。改为转换为小写。

input = in.nextLine().toLowerCase();

你可能还想在HELP命令中添加break

case HELP:
    System.out.println(HELP);
    break;
英文:

Your commands are all lower case, but you're transforming the user input into upper case. Transform into lower case instead.

input = in.nextLine().toLowerCase();

You may also want to add break to the HELP command:

             case HELP:
System.out.println(HELP);
break;

答案2

得分: 1

  1. HELPcase中缺少break语句。请注意,最后一个case中的default情况下不需要break
  2. case中的值应与字符串常量的值匹配,因此从返回的字符串中删除toUpperCase()
  3. 另外,使用do...while来避免两次使用comm = getCommand(in);

以下是已更正的代码:

import java.util.Scanner;

public class Main {
    // 常量定义命令
    public static final String REGISTER = "register";
    public static final String LISTUSERS = "listusers";
    public static final String UPLOAD = "upload";
    public static final String READ = "read";
    public static final String WRITE = "write";
    public static final String GRANT = "grant";
    public static final String REVOKE = "revoke";
    public static final String USERDOCS = "userdocs";
    public static final String TOPLEAKED = "topleaked";
    public static final String TOPGRANTERS = "topgranters";
    public static final String HELP = "help";
    public static final String EXIT = "exit";

    // 常量定义用户消息
    public static final String EXIT_COMM = "Bye!";
    public static final String REGISTER_COMM = " was registered.";
    public static final String ALREADY_REG_ERROR = " is already assigned to another user.";
    public static final String NO_LIST_USERS = "There are no registered users.";
    public static final String DOC_UPLOADED = " was uploaded.";
    public static final String OFFICIAL_DOC = "Contact updated.";
    public static final String TOPSECRET_DOC = "Contact book empty.";
    public static final String CONFIDENTIAL_DOC = "Phone number does not exist.";
    public static final String CONTACT_SHARE_PHONE = "There are contacts that share phone numbers.";
    public static final String HELP_COMM = "register - registers a new user /n";

    public static void main(String[] args) {
        System.out.println("Welcome!");
        // WeKeepSecrets app = new WeKeepSecretsClass();
        Scanner in = new Scanner(System.in);
        String comm;

        do {
            System.out.print("Enter command: ");
            comm = getCommand(in);
            switch (comm) {
                case HELP:
                    System.out.println(HELP);
                    break;
                default:
                    if (!comm.equals(EXIT)) {
                        System.out.println("ERRO");
                    }
            }
        } while (!comm.equals(EXIT));
        System.out.println(EXIT_COMM);
    }

    private static String getCommand(Scanner in) {
        String input;
        input = in.nextLine();
        return input;
    }
}

一个示例运行:

Welcome!
Enter command: help
help
Enter command: abc
ERRO
Enter command: exit
Bye!

如有疑问/问题,请随时评论。

英文:
  1. break is missing in case for HELP. Note that you do not need break in the last case which is default.
  2. The value in case should match the value of string constants, therefore, remove toUpperCase() from returned string.
  3. Also, use do...while to avoid using comm = getCommand(in); twice.

Given below is the corrected code:


import java.util.Scanner;
public class Main {
// Constantes que definem os comandos
public static final String REGISTER = "register";
public static final String LISTUSERS = "listusers";
public static final String UPLOAD = "upload";
public static final String READ = "read";
public static final String WRITE = "write";
public static final String GRANT = "grant";
public static final String REVOKE = "revoke";
public static final String USERDOCS = "userdocs";
public static final String TOPLEAKED = "topleaked";
public static final String TOPGRANTERS = "topgranters";
public static final String HELP = "help";
public static final String EXIT = "exit";
// Constantes que definem as mensagens para o utilizador
public static final String EXIT_COMM = "Bye!";
public static final String REGISTER_COMM = " was registered.";
public static final String ALREADY_REG_ERROR = " is already assigned to another user.";
public static final String NO_LIST_USERS = "There are no registered users.";
public static final String DOC_UPLOADED = " was uploaded.";
public static final String OFFICIAL_DOC = "Contact upda1ted.";
public static final String TOPSECRET_DOC = "Contact book empty.";
public static final String CONFIDENTIAL_DOC = "Phone number does not exist.";
public static final String CONTACT_SHARE_PHONE = "There are contacts that share phone numbers.";
public static final String HELP_COMM = "register - registers a new user /n";
public static void main(String[] args) {
System.out.println("Welcome!");
// WeKeepSecrets app = new WeKeepSecretsClass();
Scanner in = new Scanner(System.in);
String comm;
do {
System.out.print("Enter command: ");
comm = getCommand(in);
switch (comm) {
case HELP:
System.out.println(HELP);
break;
default:
if (!comm.equals(EXIT)) {
System.out.println("ERRO");
}
}
} while (!comm.equals(EXIT));
System.out.println(EXIT_COMM);
}
private static String getCommand(Scanner in) {
String input;
input = in.nextLine();
return input;
}
}

A sample run:

Welcome!
Enter command: help
help
Enter command: abc
ERRO
Enter command: exit
Bye!

Feel free to comment in case of any doubt/issue.

huangapple
  • 本文由 发表于 2020年4月5日 07:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61036070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定