java的return变数?

来源:百度知道 编辑:UC知道 时间:2024/05/15 16:38:08
我这两个档
想要有package和import的关系
但return会错
想知道是什么问题

两个错误讯息
1. 找不到int crc = p.outputa(strs);
2. cannot return a value from method whose result type is void

请各位解答…

package dir;
public class CRC16 {
public static void main(String[] args) {
int[] table = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
//…略 };

byte[] bytes = args[0].getBytes();
int crc = 0x0000;
for (byte b : bytes)
{
crc = (crc >>> 8) ^ table[(crc ^ b) & 0xff];
}
System.out.println("CRC16 = " + Integer.toHexString(crc));
return crc;
}
}

import dir.*;
public class im {
public static void main(String[] args) {
String []strs = {args[0]};
CRC16 p = new CRC16();
int crc = p.outputa(strs);
System.out.println(crc);
}
}

//此档放到dir package之外:
import dir.CRC16;

public class IM {
public static void main(String[] args) {
String[] strs = { args[0] };
CRC16 p = new CRC16();
p.printOutput(strs);
}
}

///////////////////
//此档放到dir package里面:

package dir;

public class CRC16 {

public void printOutput(String[] args) {
int[] table = { 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280,
0xC241 };
byte[] bytes = args[0].getBytes();
int crc = 0x0000;
for (byte b : bytes) {
crc = (crc >>> 8) ^ table[(crc ^ b) & 0xff];
}
System.out.println("CRC16 = " + Integer.toHexString(crc));
}
}

把你public class CRC16 { 这个类里的main方法改成 返回一个int类型的方法
,比如 public int outputa(){}
不过,看你下面的代码好像还要往这个方法里传个参数

两个错误讯息
1. 找不到int crc = p.outputa(strs)
since class CRC16 has error and thus cannot be compiled.

2.