我有一个名为Editor.java的程序,它几乎完成但我需要完成public void doRun(String Line)。如何运行应该是当你键入RUN时它将启动程序在它可以找到的最低编号行执行。如果RUN后跟一个35之类的数字,它应该从它找到的大于或等于指定数字的第一个行号开始。例如,如果我输入:
5 LET A = 5
10 PRINT A
20 LET X = 10
30 LET Y = 20
40 PRINT X + Y
50 LET Z = X + Y * 2
60 PRINT Z
70 GOTO 90
80 LET Z = 10
90 PRINT X Y Z
100 LET P = 80
110 IF (Z > 20) THEN LET P = 10
120 PRINT P
RUN 15
输出应为:
X + Y = 30.0
Z : 50.0
X Y Z : 10.0 20.0 50.0
P : 10.0
这是我迄今为止开发的代码如果有人可以提供帮助,我们将不胜感激。
import java.util.Scanner;
import java.io.*;
public class Editor
{
EditLL list;
HashTable commands;
HashTable variables;
final static String comm[] = {"LOAD", "SAVE", "RESEQUENCE", "LIST", "QUIT", "EXIT", "LET", "PRINT", "INPUT", "RUN"};
public Editor()
{
list = new EditLL();
commands = new HashTable();
variables = new HashTable();
for (int i = 0; i < comm.length; i++)
{
commands.insert(comm[i], (double)i);
}
}
public boolean processLine(String line) throws IOException
{
boolean retval = true;
line = trimLeadingWhitespace(line);
int last = line.indexOf(' ');
String initial = line;
if (last >= 0)
initial = line.substring(0, last);
if (last >= 0)
{
line = line.substring(last);
line = trimLeadingWhitespace(line);
}
else
line = "";
if (isInt(initial))
{
int lineNum = Integer.parseInt(initial);
list.insert(lineNum, line);
}
else
{
int key = (int)commands.getValue(initial);
switch(key)
{
case 0: this.load(line);
break;
case 1: this.save(line);
break;
case 2: list.resequence(10);
break;
case 3: System.out.println(this.list);
break;
case 4:
case 5: retval = false;
break;
case 6: doLet(line); // line after the word let
break;
case 7: print(line, variables);
break;
case 8: input(line);
break;
case 9: doRun(line);
default: System.out.println("Unknown command " + initial);
}
}
return retval;
}
public void doRun(String line) // Need Help here
{
int linenum;
int temp;
line = trimLeadingWhitespace(line);
line = trimTrailingWhitespace(line);
}
public void input(String line)
{
Scanner kb = new Scanner(System.in);
line = trimLeadingWhitespace(line);
line = trimTrailingWhitespace(line);
System.out.print(line + "=?");
variables.insert(line, kb.nextDouble());
}
public void doLet(String line)
{
int last = line.indexOf('=');
String lhs = line.substring(0, last);
String lhs2 = "";
int i = 0;
char c = lhs.charAt(i);
while(!Character.isWhitespace(c))
{
lhs2 += c;
i++;
c = lhs.charAt(i);
}
line = line.substring(last+1);
double value = Infix.evaluate(line, variables);
variables.insert(lhs2, value);
}
public void print(String line, HashTable variables)
{
line = trimLeadingWhitespace(line);
double value = Infix.evaluate(line, variables);
System.out.println(line + " = " + value);
}
public void load(String line) throws IOException
{
Scanner in = new Scanner(new File(line));
list = new EditLL();
int nextLine = 0;
while(in.hasNext())
{
nextLine += 10;
list.insert(nextLine, in.nextLine());
}
in.close();
}
public void save(String line) throws IOException
{
PrintWriter out = new PrintWriter(new File(line));
String nextline = list.getFirst();
while (!nextline.equals(""))
{
out.println(nextline);
nextline = list.getNext();
}
out.close();
}
public static boolean isInt(String s)
{
boolean retval;
try
{
Integer.parseInt(s);
retval = true;
}
catch (NumberFormatException e)
{
retval = false;
}
return retval;
}
public static String trimLeadingWhitespace(String s)
{
String retval = "";
int i = 0;
char c = s.charAt(i);
while (Character.isWhitespace(c))
{
i++;
c = s.charAt(i);
}
for (int j = i; j < s.length(); j++)
{
retval += c = s.charAt(j);
}
return retval;
}
public static String trimTrailingWhitespace(String s)
{
String retval = "";
int lastNonW = -1;
char c;
for (int j = 0; j < s.length(); j++)
{
c = s.charAt(j);
if (!Character.isWhitespace(c))
{
lastNonW = j;
}
}
for (int j = 0; j <= lastNonW; j++)
retval += s.charAt(j);
return retval;
}
public static void main(String args[]) throws IOException // Help Here
{
Editor ed = new Editor();
Scanner kb = new Scanner(System.in);
String line;
do
{
line = kb.nextLine().toUpperCase();
}while (ed.processLine(line));
}
}
答案 0 :(得分:0)
你有更大的问题:你还没有实现GOTO。
如果你实现GOTO,那么你可以通过在指令列表的开头有一个0 GOTO 35
的虚拟指令或者用户输入的任何内容来解决你的问题。
哦,一个合适的GOTO需要能够跳回来......
欢迎使用StackOverflow!