扫描并 运行 将多个输入合二为一 运行
Scanning and running multiple inputs in one run
我正在尝试编写图灵机的基本功能代码。
到目前为止,该程序接受用户输入并将其存储到列表中,如下所示:
public String cmdLoop()
{
Scanner getReq = new Scanner( System.in );
for( ; ; )
{
say( "current read/write head position: " + currHeadPos );
say( "" );
SecondMenu();
say( "Type commands here:");
ArrayList<String>inputs = new ArrayList<String>();
String userInput = getReq.nextLine();
do{
userInput = getReq.nextLine();
inputs.add(userInput);
RunCmd(userInput);
}
while(!userInput.equals("Done"));
}
}
二级菜单如下
public void SecondMenu()
{
say( "\t?\tprint current cell" );
say( "\t=\tassign new symbol to current cell" );
say( "\tE\terase current cell" );
say( "\tL\tmove head to left" );
say( "\tR\tmove head to right" );
say( "\tB\trewind to beginning of tape" );
say( "\tD\tdump contents of tape" );
say( "\tT\tset Transition commands" );
}
public void say( String s )
{
System.out.println( s );
}
每个输入都是一个命令。换句话说,程序如何 运行 取决于用户输入的内容,而 运行 执行这些命令的方法称为 RunCmd。下面是一些命令示例。
void RunCmd(String userInput)
{ char command = userInput.charAt(0);
say("");
if(command == '?')
{
dumpTape(currHeadPos, currHeadPos + 1);
}
else
if(command == '=')
{
tapeContents[currHeadPos] = userInput.charAt(1);
if( endOfTape == 0 )
{
endOfTape++;
}
}
else
if( command == 'E' )
{
//use a space to signal 'empty' so that cells will print out ok
tapeContents[ currHeadPos ] = ' ';
}
else
if( command == 'L' )
{
if( currHeadPos == 0 )
{
say( "" );
say( "Tape rewound, unable to move LEFT" );
}
else
{
currHeadPos--;
}
}
else
if( command == 'R' )
{
currHeadPos++;
endOfTape++;
}
如何让程序遍历循环并一次 运行 所有输入命令?
例如:
用户输入=1,R,=0,R
程序会使 header 下的单元格 = 1,向右移动,= 0,再次向右移动,全部合而为一 运行。
*我不希望程序在每次输入后都要求输入命令,而键入 'Done' 将结束输入。我不希望程序在程序启动时以外的任何时间显示 SecondMenu。
*我希望能够在程序 运行s 后输入 100 个输入,将它们存储在列表中,并让程序遍历数组和 运行 所有 100 个命令(基于用户输入)合二为一 运行。
我尝试过使用 for 循环、while 循环和迭代器(虽然我不知道如何使用它并且可能做错了)
编辑以澄清。
"Premature optimization is the root of all evil"
你的问题让我很困惑,但我希望这能对你有所帮助:
public static void main(String[] args)
{
Scanner getReq = new Scanner( System.in );
List<String> commands = new ArrayList<String>();
String command;
while (!(command = getReq.nextLine().trim()).equalsIgnoreCase("end"))
{
commands.add(command);
}
runCmds(commands);
}
private static void runCmds(List<String> userInputs)
{
for (String userInput : userInputs)
{
//your insanity here :)
}
}
我正在尝试编写图灵机的基本功能代码。 到目前为止,该程序接受用户输入并将其存储到列表中,如下所示:
public String cmdLoop()
{
Scanner getReq = new Scanner( System.in );
for( ; ; )
{
say( "current read/write head position: " + currHeadPos );
say( "" );
SecondMenu();
say( "Type commands here:");
ArrayList<String>inputs = new ArrayList<String>();
String userInput = getReq.nextLine();
do{
userInput = getReq.nextLine();
inputs.add(userInput);
RunCmd(userInput);
}
while(!userInput.equals("Done"));
}
}
二级菜单如下
public void SecondMenu()
{
say( "\t?\tprint current cell" );
say( "\t=\tassign new symbol to current cell" );
say( "\tE\terase current cell" );
say( "\tL\tmove head to left" );
say( "\tR\tmove head to right" );
say( "\tB\trewind to beginning of tape" );
say( "\tD\tdump contents of tape" );
say( "\tT\tset Transition commands" );
}
public void say( String s )
{
System.out.println( s );
}
每个输入都是一个命令。换句话说,程序如何 运行 取决于用户输入的内容,而 运行 执行这些命令的方法称为 RunCmd。下面是一些命令示例。
void RunCmd(String userInput)
{ char command = userInput.charAt(0);
say("");
if(command == '?')
{
dumpTape(currHeadPos, currHeadPos + 1);
}
else
if(command == '=')
{
tapeContents[currHeadPos] = userInput.charAt(1);
if( endOfTape == 0 )
{
endOfTape++;
}
}
else
if( command == 'E' )
{
//use a space to signal 'empty' so that cells will print out ok
tapeContents[ currHeadPos ] = ' ';
}
else
if( command == 'L' )
{
if( currHeadPos == 0 )
{
say( "" );
say( "Tape rewound, unable to move LEFT" );
}
else
{
currHeadPos--;
}
}
else
if( command == 'R' )
{
currHeadPos++;
endOfTape++;
}
如何让程序遍历循环并一次 运行 所有输入命令? 例如:
用户输入=1,R,=0,R
程序会使 header 下的单元格 = 1,向右移动,= 0,再次向右移动,全部合而为一 运行。
*我不希望程序在每次输入后都要求输入命令,而键入 'Done' 将结束输入。我不希望程序在程序启动时以外的任何时间显示 SecondMenu。
*我希望能够在程序 运行s 后输入 100 个输入,将它们存储在列表中,并让程序遍历数组和 运行 所有 100 个命令(基于用户输入)合二为一 运行。 我尝试过使用 for 循环、while 循环和迭代器(虽然我不知道如何使用它并且可能做错了)
编辑以澄清。
"Premature optimization is the root of all evil"
你的问题让我很困惑,但我希望这能对你有所帮助:
public static void main(String[] args)
{
Scanner getReq = new Scanner( System.in );
List<String> commands = new ArrayList<String>();
String command;
while (!(command = getReq.nextLine().trim()).equalsIgnoreCase("end"))
{
commands.add(command);
}
runCmds(commands);
}
private static void runCmds(List<String> userInputs)
{
for (String userInput : userInputs)
{
//your insanity here :)
}
}