难以理解和纠正错误消息

时间:2014-10-07 22:06:48

标签: java compiler-errors

我已经尝试了几分钟来纠正添加extractOperand方法后代码中出现的错误。我找不到任何语法或逻辑错误。此外,我注意到extractOpcode不会产生错误,而方法实际上是相同的。我现在的猜测是错误与函数的放置有关。

import java.util.*;
import java.lang.*;
import java.io.*;


public class Micro86 {
    static int[] Memory = new int[20];
    static int accumulator = 0,
                instruction_pointer = 0,
                flags = 0,
                instruction_register = 0;

    public static void main(String[] args)
    {
        String m86File = args[0];

        bootUp();
        loader(m86File);
        memoryDump();
    }

    public static void bootUp()
   {
        for(int i : Memory)
            i = 0;
   }

    public static void memoryDump()
    {
        for(int i: Memory)
            System.out.println(left_pad_zeros(Integer.toHexString(i)));
    }

    public static String registerDump()
    {
        return "Registers acc: " + accumulator
               + " ip: " + instruction_pointer
               + " flags: " + flags
               + "ir: " + instruction_register + "\n";
    }

    public static void loader(String file)
    {

        try{
            Scanner sc = new Scanner(new File(file));
            for(int i = 0; sc.hasNextInt() ; ++i){
                Memory[i] = sc.nextInt(16);
            }

        }catch(Exception e) {
            System.out.println("Cannot open file");
            System.exit(0);
        }
    }

    public static int extractOpCode(int instruction)
    {
        return instruction >>> 16;
    }

    public static String left_pad_zeros(String str)
    {
        while(str.length() < 8)
            str = "0" + str;
        return str;
    }

    public static int extractOperand(int instruction)
    {
        return instruction <<< 16;
    }

}

ERROR:

Micro86.java:71: illegal start of type
    return instruction <<< 16;
                           ^
Micro86.java:71: illegal start of expression
        return instruction <<< 16;
                                 ^
    Micro86.java:71: ';' expected
            return instruction <<< 16;
                                  ^
    Micro86.java:74: reached end of file while parsing
    }
     ^
4 errors

1 个答案:

答案 0 :(得分:5)

extractOperand方法的这一行:

return instruction <<< 16;

即使存在无符号右移运算符>>>,也没有无符号左移运算符<<<,因为向左移位时没有符号扩展要执行。您可以使用普通的左移运算符<<

return instruction << 16;