Java中怎样才能在主函数A里调用其他函数B?

比如我在B中将表达式规范化,A调用B将自己的表达式进行规范,最后println一个正确的表达式。我尝试了new一个B,但最后得到的是空值,即A中的表达式没有传进B中进行处理。
public class ExpCal {
static char OPSET[] = { '+', '-', '*', '/', '^', '(', ')', '#' };
static char Prior[][] = { { '>', '>', '<', '<', '<', '<', '>', '>' },
{ '>', '>', '<', '<', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '=', '<', '>', '>' },
{ '<', '<', '<', '<', '<', '<', '=', ' ' },
{ '>', '>', '>', '>', '>', ' ', '>', '>' },
{ '<', '<', '<', '<', '<', '<', ' ', '=' } };
String infix = "";

ExpCal() {

}

public static String Trim(String s) {
char charset[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '+', '-', '*', '/', '(', ')', '^', '.' };
// legal chars
int i = 0;
char ch;
String correct_exp = "8&8";

while (i < s.length()) { // 滤掉非法字符
ch = s.charAt(i++);
if (charset[i] == ch)
correct_exp += ch;
}
public static double operate(String operator, String a, String b) {
double x = Double.parseDouble(a), y = Double.parseDouble(b);
if (operator.equals("+"))
return x + y;
if (operator.equals("-"))
return x - y;
if (operator.equals("*"))
return x * y;
if (operator.equals("/"))
return x / y;
if (operator.equals("^"))
return Math.pow(x, y);
throw new IllegalArgumentException("Unknown operator: " + operator);
}

public static boolean isArithmeticOperator(String s) {
return s.length() == 1 && "+-*/^".indexOf(s) != -1;
}

public static void main(String args[]) {
System.out.println(Trim("12"));
}
}

首先,你发布的代码是错误的。
其次,根据一楼和本人的判断,你逻辑上有错误。
if (charset[i] == ch)
correct_exp += ch;
这里你老是拿数组里的第一个到字符串长度的N-1个字符来和charset里的前N-1项比较,会有结果吗?
public class ExpCal {
static char OPSET[] = { '+', '-', '*', '/', '^', '(', ')', '#' };
static char Prior[][] = { { '>', '>', '<', '<', '<', '<', '>', '>' },
{ '>', '>', '<', '<', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '=', '<', '>', '>' },
{ '<', '<', '<', '<', '<', '<', '=', ' ' },
{ '>', '>', '>', '>', '>', ' ', '>', '>' },
{ '<', '<', '<', '<', '<', '<', ' ', '=' } };
String infix = "";

ExpCal() {

}

public static boolean isInCharSet(char[] arr,char c) {
for(int i=0;i<arr.length;i++) {
if(arr[i]==c) {
return true;
}
}
return false;
}

public static String Trim(String s) {
char charset[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '+', '-', '*', '/', '(', ')', '^', '.' };
// legal chars
int i = 0;
char ch;
String correct_exp = "8&8";

while (i < s.length()) { // 滤掉非法字符
ch = s.charAt(i++);
if (isInCharSet(charset,ch))
correct_exp += ch;
}
return correct_exp;
}
public static double operate(String operator, String a, String b) {
double x = Double.parseDouble(a), y = Double.parseDouble(b);
if (operator.equals("+"))
return x + y;
if (operator.equals("-"))
return x - y;
if (operator.equals("*"))
return x * y;
if (operator.equals("/"))
return x / y;
if (operator.equals("^"))
return Math.pow(x, y);
throw new IllegalArgumentException("Unknown operator: " + operator);
}

public static boolean isArithmeticOperator(String s) {
return s.length() == 1 && "+-*/^".indexOf(s) != -1;
}

public static void main(String args[]) {
System.out.println(Trim("12"));
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-10-14
如果B函数是static静态的,直接类名点函数名就行,如果不是,先把B所在类new出一个实例来,再用实例去点,最后别忘了把B所在类import到A中哦
第2个回答  2011-04-25
Java中怎样才能在主函数A里调用其他函数B?
把B方法写成static方法,或者用类来调用!本回答被提问者采纳
第3个回答  2011-04-25
在trim()函数里,if (charset[i] == ch) correct_exp += ch; 这里的charset[i]是不是只获取到一个字符和ch进行比较?考虑下~~~此处
相似回答