A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张雪磊 中级黑马   /  2012-7-7 20:18  /  1959 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张雪磊 于 2012-7-7 22:41 编辑
  1. <blockquote>class ArrayTest
复制代码
运行的结果居然是:4546474849505152535455
这是怎么个情况啊?

7 个回复

倒序浏览

重发代码

  1. class ArrayTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10,11};
  6.                 parr(arr);
  7.         }

  8.         public static void parr(int[] x)
  9.         {
  10.                 for (int i=0;i<x.length ;i++ )
  11.                 {
  12.                         System.out.print(x[i]+',');
  13.                 }
  14.         }
  15. }
复制代码
奇怪明明复制进去代码了,为什么就剩下<blockquote>class ArrayTest了???

点评

楼主要多多注意一下代码,输出语句中单引号的错误!  发表于 2012-7-8 00:41
回复 使用道具 举报
本帖最后由 张天天 于 2012-7-8 08:14 编辑

package test;

class ArrayTest

{

        public static void main(String[] args)

        {

                int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10,11};

                parr(arr);

        }


        public static void parr(int[] x)

        {

                for (int i=0;i<x.length ;i++ )

                {

                        System.out.print(x+",");

                }

        }

}
执行结果是
1,2,3,4,5,6,7,8,9,10,11,一定是后面的那个单引号出的问题啊

单引号引的数据 是char类型的 双引号引的数据 是String类型的 单引号只能引一个字符 而双引号可以引0个及其以上 在这里的单引号完全可以换成双引号System.out.print(0+',');输出的结果是44,所以我们结果是45至55
回复 使用道具 举报
本帖最后由 朱东方 于 2012-7-7 20:50 编辑
  1. class ArrayTest

  2. {

  3.         public static void main(String[] args)

  4.         {

  5.                 int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10,11};

  6.                 parr(arr);

  7.         }


  8.         public static void parr(int[] x)

  9.         {

  10.                 for (int i=0;i<x.length ;i++ )

  11.                 {

  12.                         System.out.print(x[i]+",");//双引号","。。。x[i]+','  这是求和了,','对应的十进制数是44。结果就变成他俩和了。

  13.                 }

  14.         }

  15. }
复制代码
回复 使用道具 举报
3楼说的很对,用单引号就意味着把int型和char型进行加法运算,用双引号才是字符串相连接,问题出在这里。这一点要谨慎。
希望帮到你~~
回复 使用道具 举报
class ArrayTest

{

        public static void main(String[] args)

        {

                int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10,11};

                parr(arr);

        }



        public static void parr(int[] x)

        {

                for (int i=0;i<x.length ;i++ )

                {

                        System.out.print(x+','); //双引号表示字符串,单引号表示字符!如果想要达到输出正确的数请把单引号换成双引号!!
                }

        }

}
回复 使用道具 举报
曹恒业 发表于 2012-7-7 20:46
3楼说的很对,用单引号就意味着把int型和char型进行加法运算,用双引号才是字符串相连接,问题出在这里。这 ...

我试了半天才试出来,这错误一定不能犯。要不很麻烦。
回复 使用道具 举报
public static void parr(int[] x)
        {
                for (int i=0;i<x.length ;i++ )
                {
                        System.out.print(x+','); //单引号的话是一个具体的字符常量,表达式就会进行加运算,楼主要细心
                }
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马