Java 编译器错误,因为主方法不是静态的
我编写了一个程序,将一些文本汇编成二进制代码。编译器总是在main方法中给出一个错误,并告诉我它不是静态的…有人能帮我吗???? 我试图使它静态,但错误仍然存在。我希望有人能帮助我Java 编译器错误,因为主方法不是静态的,java,Java,我编写了一个程序,将一些文本汇编成二进制代码。编译器总是在main方法中给出一个错误,并告诉我它不是静态的…有人能帮我吗???? 我试图使它静态,但错误仍然存在。我希望有人能帮助我 public class Main { private String file; private int pc = 0; public Main( String fin ) { file = fin; } public void main( Strin
public class Main {
private String file;
private int pc = 0;
public Main( String fin ) {
file = fin;
}
public void main( String[] args ) throws IOException, Exception {
// CREATE INSTANCES OF OTHER MODULES
Parser fp = new Parser( args[ 0 ] );
Parser sp = new Parser( args[ 0 ] );
Code code = new Code();
HashMap<String, String> st = new HashMap<String, String>();
// SYMBOL TABLE INITIALIZATION
st.put( "R0", "0" ); st.put( "R1", "1" ); st.put( "R2", "2" ); st.put( "R3", "3" ); st.put( "R4", "4" ); st.put( "R5", "5" ); st.put( "R6", "6" ); st.put( "R7", "7" );
st.put( "R8", "8" ); st.put( "R9", "9" ); st.put( "R10", "10" ); st.put( "R11", "11" ); st.put( "R12", "12" ); st.put( "R13", "13" ); st.put( "R14", "14" ); st.put( "R15", "15" );
st.put( "SCREEN", "16384" ); st.put( "KBD", "24576" );
st.put( "SP", "0" ); st.put( "LCL", "1" ); st.put( "ARG", "2" ); st.put( "THIS", "3" ); st.put( "THAT", "4" );
// FIRST PASS
fp.advance();
while( fp.command != null ) {
if( fp.commandType() == "L_COMMAND" ) {
st.put( fp.symbol(), Integer.toString( pc ) );
pc--;
}
fp.advance();
pc++;
}
// SECOND PASS
FileWriter writer = null;
int rAllocation = 16; // Keeps a record of the last register allocated to a variable.
try {
// CREATE FILE, FILE WRITER
File nf = new File( file.replaceAll( "\\.asm", ".hack" ) );
nf.createNewFile();
writer = new FileWriter( nf );
// SECOND PASS
sp.advance();
while( sp.command != null ) {
if( sp.commandType() == "L_COMMAND" ) {
// Do nothing.
} else if( sp.commandType() == "A_COMMAND" ) {
if( !( Pattern.compile( "[a-zA-Z]" ).matcher( sp.symbol() ).find() ) ) { // If the symbol consists of only digits.
writer.write( convertAddr( sp.symbol() ) + "\n" ); // Translate integer value to binary, write to file.
} else if( st.get( sp.symbol() ) == null ){
st.put( sp.symbol(), Integer.toString( rAllocation ) ); // Assign the variable an unoccupied register.
rAllocation++;
writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" ); // Retrieve the just allocated value from SymbolTable, translate to binary, write.
} else {
writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" ); // Retrieve value of symbol from SymbolTable, translate to binary, write.
}
} else if( sp.commandType() == "C_COMMAND" ) {
String d = code.dest( sp.dest() );
String c = code.comp( sp.comp() );
String j = code.jump( sp.jump() );
writer.write( "111" + c + d + j + "\n" );
}
sp.advance();
}
} catch( IOException e ) {
e.printStackTrace();
} finally {
// CLOSE WRITER
writer.flush();
writer.close();
}
}
private String convertAddr( String addr ) throws Exception{
String bin;
String zeroPad = "";
if( addr != null ) {
bin = Integer.toBinaryString( Integer.parseInt( addr ) );
for( int i = 0; i < ( 16 - bin.length() ); i++ ) {
zeroPad += "0";
}
return zeroPad + bin;
} else {
throw new Exception( "Null Parameter." );
}
}
}
公共类主{
私有字符串文件;
私有整数pc=0;
公用干管(串鳍){
文件=fin;
}
public void main(字符串[]args)引发IOException,异常{
//创建其他模块的实例
Parser fp=新的解析器(args[0]);
Parser sp=新的解析器(args[0]);
代码=新代码();
HashMap st=新的HashMap();
//符号表初始化
标准看跌期权(“R0”、“0”);标准看跌期权(“R1”、“1”);标准看跌期权(“R2”、“2”);标准看跌期权(“R3”、“3”);标准看跌期权(“R4”、“4”);标准看跌期权(“R5”、“5”);标准看跌期权(“R6”、“6”);标准看跌期权(“R7”、“7”);
标的卖出期权(“R8”、“8”);标的卖出期权(“R9”、“9”);标的卖出期权(“R10”、“10”);标的卖出期权(“R11”、“11”);标的卖出期权(“R12”、“12”);标的卖出期权(“R13”、“13”);标的卖出期权(“R14”、“14”);标的卖出期权(“R15”、“15”);
圣普特(“屏幕”,“16384”);圣普特(“KBD”,“24576”);
标的卖出期权(“SP”、“0”);标的卖出期权(“LCL”、“1”);标的卖出期权(“ARG”、“2”);标的卖出期权(“本”、“3”);标的卖出期权(“该”、“4”);
//第一关
fp.advance();
while(fp.command!=null){
if(fp.commandType()=“L_命令”){
st.put(fp.symbol(),Integer.toString(pc));
个人电脑--;
}
fp.advance();
pc++;
}
//第二关
FileWriter=null;
int-rAllocation=16;//保存分配给变量的最后一个寄存器的记录。
试一试{
//创建文件、文件编写器
File nf=新文件(File.replaceAll(\\\.asm,“.hack”);
nf.createNewFile();
writer=新文件编写器(nf);
//第二关
sp.advance();
while(sp.command!=null){
如果(sp.commandType()=“L_命令”){
//什么也不做。
}else if(sp.commandType()=“A_命令”){
if(!(Pattern.compile(“[a-zA-Z]”)matcher(sp.symbol()).find()){//如果符号仅由数字组成。
writer.write(convertAddr(sp.symbol())+“\n”);//将整数值转换为二进制,写入文件。
}else if(st.get(sp.symbol())==null){
st.put(sp.symbol(),Integer.toString(rAllocation));//为变量分配一个未占用的寄存器。
rAllocation++;
write(convertAddr(st.get(sp.symbol())+“\n”);//从SymbolTable中检索刚刚分配的值,转换为二进制,然后写入。
}否则{
writer.write(convertAddr(st.get(sp.symbol())+“\n”);//从SymbolTable中检索符号值,转换为二进制,然后写入。
}
}else if(sp.commandType()=“C_命令”){
字符串d=code.dest(sp.dest());
字符串c=code.comp(sp.comp());
字符串j=code.jump(sp.jump());
writer.write(“111”+c+d+j+”\n”);
}
sp.advance();
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
//亲密作家
writer.flush();
writer.close();
}
}
私有字符串convertAddr(字符串地址)引发异常{
串箱;
字符串zeroPad=“”;
if(addr!=null){
bin=Integer.toBinaryString(Integer.parseInt(addr));
对于(int i=0;i<(16-bin.length());i++){
零键盘+=“0”;
}
返回zeroPad+bin;
}否则{
抛出新异常(“Null参数”);
}
}
}
还有另外两个类,parser和code,它们与主类相关。主方法必须是静态的,这样就可以在不创建主类实例的情况下调用主方法。只需更改为静态。您的主要方法需要
public static void main(String[] args){}
之所以需要静态方法,是因为对类的对象调用了非静态方法,并且在程序开始时还没有创建任何对象。但是,静态方法是在类上调用的,这意味着可以在类加载后立即调用它们,但只能访问类的其他静态资源
这意味着您的pc
和文件
变量也需要是静态的,或者您需要将代码移动到不同的方法,并在主方法中创建一个主对象,然后在新对象上调用该新方法,如下所示:
public class Main(){
private String file;
private int pc = 0;
public Main(String fin){
file=fin;
}
public static void main(String[] args){
Main main = new Main(args[0]); //or wherever the filename is in args
main.doStuff();
}
public void doStuff(){
//your code
}
}
将变量和主方法设置为静态,如下所示。应该行得通
public class Main {
private static String file;
private static int pc = 0;
public Main( String fin ) {
file = fin;
}
public static void main( String[] args ) throws Exception {
// CREATE INSTANCES OF OTHER MODULES
Parser fp = new Parser( args[ 0 ] );
Parser sp = new Parser( args[ 0 ] );
Code code = new Code();
HashMap<String, String> st = new HashMap<String, String>();
// SYMBOL TABLE INITIALIZATION
st.put( "R0", "0" ); st.put( "R1", "1" ); st.put( "R2", "2" ); st.put( "R3", "3" ); st.put( "R4", "4" ); st.put( "R5", "5" ); st.put( "R6", "6" ); st.put( "R7", "7" );
st.put( "R8", "8" ); st.put( "R9", "9" ); st.put( "R10", "10" ); st.put( "R11", "11" ); st.put( "R12", "12" ); st.put( "R13", "13" ); st.put( "R14", "14" ); st.put( "R15", "15" );
st.put( "SCREEN", "16384" ); st.put( "KBD", "24576" );
st.put( "SP", "0" ); st.put( "LCL", "1" ); st.put( "ARG", "2" ); st.put( "THIS", "3" ); st.put( "THAT", "4" );
// FIRST PASS
fp.advance();
while( fp.command != null ) {
if( fp.commandType() == "L_COMMAND" ) {
st.put( fp.symbol(), Integer.toString( pc ) );
pc--;
}
fp.advance();
pc++;
}
// SECOND PASS
FileWriter writer = null;
int rAllocation = 16; // Keeps a record of the last register allocated to a variable.
try {
// CREATE FILE, FILE WRITER
File nf = new File( file.replaceAll( "\\.asm", ".hack" ) );
nf.createNewFile();
writer = new FileWriter( nf );
// SECOND PASS
sp.advance();
while( sp.command != null ) {
if( sp.commandType() == "L_COMMAND" ) {
// Do nothing.
} else if( sp.commandType() == "A_COMMAND" ) {
if( !( Pattern.compile( "[a-zA-Z]" ).matcher( sp.symbol() ).find() ) ) { // If the symbol consists of only digits.
writer.write( convertAddr( sp.symbol() ) + "\n" ); // Translate integer value to binary, write to file.
} else if( st.get( sp.symbol() ) == null ){
st.put( sp.symbol(), Integer.toString( rAllocation ) ); // Assign the variable an unoccupied register.
rAllocation++;
writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" ); // Retrieve the just allocated value from SymbolTable, translate to binary, write.
} else {
writer.write( convertAddr( st.get( sp.symbol() ) ) + "\n" ); // Retrieve value of symbol from SymbolTable, translate to binary, write.
}
} else if( sp.commandType() == "C_COMMAND" ) {
String d = code.dest( sp.dest() );
String c = code.comp( sp.comp() );
String j = code.jump( sp.jump() );
writer.write( "111" + c + d + j + "\n" );
}
sp.advance();
}
} catch( IOException e ) {
e.printStackTrace();
} finally {
// CLOSE WRITER
writer.flush();
writer.close();
}
}
private String convertAddr( String addr ) throws Exception{
String bin;
String zeroPad = "";
if( addr != null ) {
bin = Integer.toBinaryString( Integer.parseInt( addr ) );
for( int i = 0; i < ( 16 - bin.length() ); i++ ) {
zeroPad += "0";
}
return zeroPad + bin;
} else {
throw new Exception( "Null Parameter." );
}
}
}
公共类主{
私有静态字符串文件;
专用静态int pc=0;
公用干管(串鳍){
文件=fin;
}
公共静态void main(字符串[]args)引发异常{
//创建其他模块的实例
Parser fp=新的解析器(args[0]);
Parser sp=新的解析器(args[0]);
代码=新代码();
HashMap st=新的HashMap();
//符号表初始化
标准看跌期权(“R0”、“0”);标准看跌期权(“R1”、“1”);标准看跌期权(“R2”、“2”);标准看跌期权(“R3”、“3”);标准看跌期权(“R4”、“4”);标准看跌期权(“R5”、“5”);标准看跌期权(“R6”、“6”);标准看跌期权(“R7”、“7”);
标的卖出期权(“R8”、“8”);标的卖出期权(“R9”、“9”);标的卖出期权(“R10”、“10”);标的卖出期权(“R11”、“11”);标的卖出期权(“R12”、“12”);标的卖出期权(“R13”、“13”);标的卖出期权(“R14”、“14”);标的卖出期权(“R15”、“15”);
圣普特(“屏幕”,“16384”);圣普特(“KBD”,“24576”);
标的卖出期权(“SP”、“0”);标的卖出期权(“LCL”、“1”);标的卖出期权(“ARG”、“2”);标的卖出期权(“本”、“3”);标的卖出期权(“该”、“4”);
//冷杉