这是我的显示界面
public interface Display {
/**
* <p>
* Defines how a display decorates each line</p>
*/
public enum Decorator {
/**
* <p>
* {@code***}The default string shown around messages</p>
*/
MESSAGE("***"),
/**
* <p>
* {@code ?}The default string shown after a memory location to input an
* instruction</p>
*/
INSTRUCTION("?"),
/**
* <p>
* {@code >>}The default string shown during the execution of a
* simpletron program when requesting for input</p>
*/
INPUT(">>"),
/**
* <p>
* {@code }An empty string</p>
*/
NONE("");
private final String decorator;
private Decorator(String decorator) {
this.decorator = decorator;
}
@Override
public String toString() {
return decorator;
}
}
/**
* <p>
* Display the welcome screen</p>
*/
public void splashScreen();
/**
* <p>
* Displays a message without formatting
* </p>
*
* @param message The message to displayInline
*/
default void show(String message) {
format(message);
}
/**
* <p>
* Displays a message with formatting, works just like the
* {@code String::format method}
* </p>
*
* @param message The message to displayInline
* @param args The
*/
public void format(String message, Object... args);
public static class Messages {
/**
* <p>
* The message that greets the user when they load simpletron</p>
*/
public static final String WELCOME_MESSAGE = "Welcome to Simpletron!\n"
+ "Please enter your program one instruction\n"
+ "(or data word) at a time. I will display\n"
+ "the location number and a question mark (?)\n"
+ "You then type the word for that location.\n"
+ "Type -99999 to stop entering your program.";
public static final String READ = "Enter an integer";
public static final String HALT = "Simpletron execution terminated";
public static final String DIVIDE_BY_ZERO = "Attempt to divide by zero";
public static final String FATAL_ERROR = "Simpletron execution abnormally terminated";
}
}
这是具体的实施
public class CommandlineDisplay implements Display {
@Override
public void splashScreen() {
Arrays.stream(Messages.WELCOME_MESSAGE.split("\n")).forEach(line -> format("%s %-43s %s\n", Decorator.MESSAGE, line, Decorator.MESSAGE));
}
@Override
public void format(String message, Object... args) {
System.out.printf(message, args);
}
}
这是系统输入的实现
public class CommandlineInput implements Input {
private static final Scanner keyboard = new Scanner(System.in);
@Override
public String nextLine() {
return keyboard.nextLine();
}
}
最后但并非最不重要的是,这是主要的方法
protected void runSystem() {
display.splashScreen();
int index = 0;
int inputValue;
int memorySize = memory.getSize();
while (index < memorySize) {
display.format("%2d %s ", index, Decorator.INSTRUCTION);
inputValue = Integer.parseInt(input.nextLine());
if (inputValue == -99999)
break;
if (inputValue > 9999 || inputValue < -9999)
continue;
memory.set(index++, inputValue);
}
displayMessage("Handling operations");
while (CPU.Register.INSTRUCTION_COUNTER.getValue() < memorySize) {
handleOperation(cpu.execute(memory));
if (CPU.Register.OPERATION_CODE.getValue() < 40)
CPU.Register.INSTRUCTION_COUNTER.setValue(CPU.Register.INSTRUCTION_COUNTER.getValue() + 1);
}
}
我面临的问题是输入的命令行块没有显示display.format方法调用的消息,我只是很沮丧我已经做了我能想到的所有事情,包括将输入实现更改为缓冲读取器和用maven运行,仍然是相同的问题
暂无答案!
目前还没有任何答案,快来回答吧!