【发布时间】:2013-04-25 02:06:48
【问题描述】:
为什么这个码位中使用的三元运算会起作用?
public static void main(String [] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader( System.in ))) {
String hello;
while ((hello = br.readLine()) != null)
System.out.println ( ( hello.matches( ".*h+.*e+.*l.*l.*o+.*" ) ) ? "YES" : "NO"); //this line works
}
catch ( NullPointerException xNE ) { }
}
但它不适用于此:
private static void Print_denom( int num ) {
if ( num > 1 ) {
System.out.print(num + " ");
isEven ( num ) ? Print_denom( num>>1 ) : isPrime(num) ? Print_denom(1) : Print_denom(num/Coins.div); //this one doesn't
}
}
?
编辑: 我想我现在明白了。如果我将第二个功能更改为:
private static int Print_denom( int num ) {
if ( num > 1 ) {
System.out.print(num + " ");
return isEven ( num ) ? Print_denom( num>>1 ) : isPrime(num) ? Print_denom(1) : Print_denom(num/Coins.div);
}
return 0;
}
然后就可以了。谢谢大家
【问题讨论】:
-
似乎在这里得到了回答:stackoverflow.com/a/9451021。并且,作为参考,这里是
expression和statement在 java 中的定义:docs.oracle.com/javase/tutorial/java/nutsandbolts/…
标签: java methods conditional-operator