gpt4 book ai didi

java - 由于 main 方法不是静态的,编译器错误

转载 作者:行者123 更新时间:2023-12-01 19:31:37 25 4
gpt4 key购买 nike

我编写了一个程序来将一些文本组装成二进制代码。编译器总是在 main 方法中给出错误,并告诉我它不是静态的......有人可以帮助我吗???我试图使其静态,但错误仍然存​​在。我希望有人能帮助我。

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." );
}
}
}


还有另外两个类,解析器和代码,与主类相关

最佳答案

你的主要方法需要是

public static void main(String[] args){}

需要静态,因为非静态方法是在类的对象上调用的 - 并且在程序开始时还没有创建对象。然而,静态方法是在类上调用的,这意味着它们可以在类加载后立即调用,但只能访问类的其他静态资源。

这意味着您的 pc 和 file 变量也需要是静态的,或者您需要将代码移动到不同的方法并在 main 中创建一个 Main 对象方法并在新对象上调用该新方法,如下所示:

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
}
}

关于java - 由于 main 方法不是静态的,编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59658876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com