为什么这样写就可以输出字符
for(int i=0;i!=19;++i)    
  System.out.print(" "+(i<10?i:(char)(i+87)+" "));
而这样写就输不出字符
for(int i=0;i!=19;++i)    
  System.out.print(" "+(i<10?i:(char)(i+87))+" ");是不是第一种都转为String类型的了?

解决方案 »

  1.   

    怎么又是这东西?上次在你的那个帖子中我在 13 楼已经把 JSL 中关于三目运算符所有的规则都列出来的了。PS:没事不要整这些让人眼晕的东西了,代码是给人看的,而不是给计算机看的。
      

  2.   

    System.out.print(" "+(i<10?i:(char)(i+87)+" "));
    相当于System.out.print(" "+(i<10?i:  ((char)(i+87)+" ")));//后面的值是个字符串,所以三目运算(i<10?i:(char)(i+87)+" ")的结果是个字符串System.out.print(" "+(i<10?i:(char)(i+87))+" ");//这个三目运算的结果是个int型(因为int型大于char型,所以char被转为int型),然后int在加上前后两个字符串一起输出
      

  3.   

    你这里还涉及运算符的优先级问题。下面是 Java 中所有 14 个优先级别 54 种运算符的列表。(弄死我了!)优先级   操作符   含义        关联性  用法 
    ----------------------------------------------------------------
    1        [ ]      数组下标      左    array_name[expr] 
             .        成员选择      左    object.member 
             ( )      方法参数      左    method_name(expr_list) 
             ( )      实例构造      左    class_name(expr_list) 
             ++       后缀自增      左    lvalue++ 
             --       后缀自减      左    lvalue-- 2        ++       前缀自增      右    ++rvalue 
             --       前缀自减      右    --lvalue 
             ~        按位取反      右    ~expr 
             !        逻辑非        右    !expr 
             +        一元加        右    +expr 
             -        一元减        右    -expr 3        ( )      强制转换      右    (type)expr 
             new      对象实例化    右    new type() 
                                          new type(expr_list) 
                                          new type[expr] 4        *        乘            左    expr * expr 
             /        除            左    expr / expr 
             %        求余          左    expr % expr 5        +        加            左    expr + expr 
             -        减            左    expr - expr 
             +        字符串连接    左    strExpr + strExpr 6        >>       有符号右移    左    expr >> distance 
             >>>      无符号右移    左    expr >>> distance 7        <        小于          左    expr < expr 
             <=       小于等于      左    expr <= expr 
             >        大于          左    expr > expr 
             >=       大于等于      左    expr >= expr 
          instanceof  类型比较      左    ref instanceof refType 
             ==       等于          左    expr == expr 
             !=       不等于        左    expr != expr 8        &        整数按位与    左    integralExpr & integralExpr 
             &        布尔与        左    booleanExpr & booleanExpr 9        ^        整数按位异或  左    integralExpr ^ integralExpr 
             ^        布尔异或      左    booleanExpr ^ booleanExpr 10       |        整数按位或    左    integralExpr | integralExpr 
             |        布尔或        左    booleanExpr | booleanExpr 11       &&       逻辑与        左    booleanExpr && booleanExpr 12       ||       逻辑或        左    booleanExpr || booleanExpr 13       ? :      条件运算      右    booleanExpr ? expr : expr 14       =        赋值          右    lvalue = expr 
             *=       乘赋值        右    lvalue *= expr 
             /=       除赋值        右    lvalue /= expr 
             %=       模赋值        右    lvalue %= expr 
             +=       加赋值        右    lvalue += expr 
             +=    字符串连接赋值   右    lvalue += expr 
             -=       减赋值        右    lvalue -= expr 
             <<=      左移赋值      右    lvalue <<= expr 
             >>=   有符号右移赋值   右    lvalue >>= expr 
             >>>=  无符号右移赋值   右    lvalue >>>= expr 
             &=    整数按位与赋值   右    lvalue &= expr 
             &=       布尔与赋值    右    lvalue &= expr 
             |=    整数按位或赋值   右    lvalue |= expr 
             |=       布尔或赋值    右    lvalue |= expr 
             ^=   整数按位异或赋值  右    lvalue ^= expr 
             ^=     布尔异或赋值    右    lvalue ^= expr 
      

  4.   

    If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.  
    If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.  If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.  If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.  If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.