Java 基于compareTo的调用方法?
如果我使用compareTo作为BigInteger,我如何从结果中选择调用哪个函数-1=funcA,+1=funcB,0=no函数 特别是:这有什么问题Java 基于compareTo的调用方法?,java,compare,biginteger,Java,Compare,Biginteger,如果我使用compareTo作为BigInteger,我如何从结果中选择调用哪个函数-1=funcA,+1=funcB,0=no函数 特别是:这有什么问题 doCompare() { BigInteger x = new BigInteger(5); BigInteger y = new BigInteger(10); //syntax error token "<", invalid assignment operator x.compareTo(y) <
doCompare() {
BigInteger x = new BigInteger(5);
BigInteger y = new BigInteger(10);
//syntax error token "<", invalid assignment operator
x.compareTo(y) < 0 ? funcA() : funcB();
}
void funcA();
void funcB();
由于funcA和funcB具有返回类型void,因此不能使用。您可以将其重写为常规if语句,但:
if (x.compareTo(y) < 0) {
funcA();
} else {
funcB();
}
由于funcA和funcB具有返回类型void,因此不能使用。您可以将其重写为常规if语句,但:
if (x.compareTo(y) < 0) {
funcA();
} else {
funcB();
}
用这个,
公共文件公司
{
BigInteger x=新的BigInteger 5;
大整数y=新的大整数10
//syntax error token "<", invalid assignment operator
if(x.compareTo(y) < 0 )
{
funcA();
}
else
{
funcB();
}
}
public void funcA()
{
}
public void funcB()
{
}
如果要在一行中使用条件,则需要修改函数:
用这个,
公共文件公司
{
BigInteger x=新的BigInteger 5;
大整数y=新的大整数10
//syntax error token "<", invalid assignment operator
if(x.compareTo(y) < 0 )
{
funcA();
}
else
{
funcB();
}
}
public void funcA()
{
}
public void funcB()
{
}
如果要在一行中使用条件,则需要修改函数:
首先,您错误地使用了BigInteger构造函数,它使用的字符串不是int或long。并且您的函数必须具有布尔返回类型,否则无法在三元运算中使用它们
void doCompare()
{
BigInteger x = new BigInteger("5");
BigInteger y = new BigInteger("10");
boolean result = x.compareTo(y) < 0 ? funcA() : funcB();
}
boolean funcA(){return true;};
boolean funcB(){return false;};
首先,您错误地使用了BigInteger构造函数,它使用的字符串不是int或long。并且您的函数必须具有布尔返回类型,否则无法在三元运算中使用它们
void doCompare()
{
BigInteger x = new BigInteger("5");
BigInteger y = new BigInteger("10");
boolean result = x.compareTo(y) < 0 ? funcA() : funcB();
}
boolean funcA(){return true;};
boolean funcB(){return false;};
为什么不将结果转换为临时值并执行if/else?因为这只是一个抽象我的问题的示例…为什么不将结果转换为临时值并执行if/else?因为这只是一个抽象我的问题的示例。。。