튜링 기계의 기본 기능을 코딩하려고합니다. 아래와 같이 지금까지, 프로그램리스트로 사용자 입력 및 저장을 취한 번의 실행으로 여러 입력 검사 및 실행
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"));
}
}
SecondMenu 각 입력 명령 인 아래
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 =이 프로그램은 마우스 오른쪽 단추로 다시 이동, 헤더 = 1에서 셀을 마우스 오른쪽 단추로 이동 = 0 것
0, R, 모든 한 번에.
* 프로그램이 각 입력 후에 명령을 요청하지 않고 '완료'를 입력하면 입력이 끝납니다. 나는 프로그램이 시작될 때 이외에 언제든지 프로그램이 SecondMenu를 표시하는 것을 원하지 않는다.
* 프로그램 실행 후 100 개의 입력을 입력하고 List에 저장하고 배열을 통해 프로그램을 반복하고 100 개의 명령 (사용자 입력 기반)을 한 번에 실행할 수 있기를 원합니다. for 루프, while 루프 및 반복기를 사용해 보았습니다. (사용법을 잘 모르고 잘못했을 수도 있지만)
설명을 위해 편집되었습니다.
을 그래서 배열에 각 입력을 추가에서 당신을 중지하는 것 또는 목록 및 배열 또는 목록을 통해 루핑 및 각 호출 RunCmd()? –
for 루프를 사용해 보았습니다. 어쩌면 내가 잘못했는지 모르겠지만 내가 원했던 방식대로 작동하지 않았다. –