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

  1. /**
  2. 此程序用来诠释数据类型转换
  3. 思路:自动类型转换例子: 短整型转换成整型 单精度转换成双精度 字符型转换成整型
  4. 强制类型转换例子: 长整型转换成整型 整形转换成字节

  5. 步骤:
  6. 1.定义一个类
  7. 2.主函数
  8. 3.声明数据类型,赋值
  9. 4.转换并输出到屏幕
  10. */
  11. class Transformation
  12. {
  13. public static void main(String args[])
  14. {
  15. int x=4;
  16. short y=3;
  17. x=x+y;//整数=整数+短整数
  18. System.out.println("x="x);
  19. float a=3.1;
  20. double b=4.3;
  21. b=b+a;//双精度小数=双精度小数+单精度小数
  22. System.out.println("b="b);
  23. System.out.println('v'+2);//ASCLL码,字符转换成整数输出
  24. long m=6;
  25. int n=3;
  26. byte b=1;
  27. n=(int)(m+n);
  28. System.out.println('n'=n);//长整型转换成整型
  29. System.out.println('n'=(byte)(n+b));//整形转换成字节
  30. }
  31. }
复制代码

这个老是显示有4个错误,不知道怎么改,还有我这么诠释字符转换合适么,总觉得哪里不对,但是又说不出来。

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 语法还需注意。加油。

查看全部评分

11 个回复

倒序浏览
何止四个错误,好好看看基础吧。
  1. /**
  2. 此程序用来诠释数据类型转换
  3. 思路:自动类型转换例子: 短整型转换成整型 单精度转换成双精度 字符型转换成整型
  4. 强制类型转换例子: 长整型转换成整型 整形转换成字节

  5. 步骤:
  6. 1.定义一个类
  7. 2.主函数
  8. 3.声明数据类型,赋值
  9. 4.转换并输出到屏幕
  10. */
  11. class Demo
  12. {
  13. public static void main(String args[])
  14. {
  15. int x=4;
  16. short y=3;
  17. x=x+y;//整数=整数+短整数
  18. System.out.println("x="+x);
  19. float a=3.1f;
  20. double b=4.3;
  21. b=b+a;//双精度小数=双精度小数+单精度小数
  22. System.out.println("b="+b);
  23. System.out.println('v'+2);//ASCLL码,字符转换成整数输出
  24. long m=6;
  25. int n=3;
  26. byte by=1;
  27. n=(int)(m+n);
  28. System.out.println("n="+n);//长整型转换成整型
  29. System.out.println("n="+(byte)(n+by));//整形转换成字节
  30. }
  31. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
水竹 发表于 2014-10-12 02:58
何止四个错误,好好看看基础吧。

输出怎么是双引号+变量,和别的语言不太一样啊。我刚看到数据转换这里迫不及待的敲了代码,可能没看到这块。不过那个双精度小数加起来不是应该是7.4吗,怎么会是这么长
回复 使用道具 举报
本帖最后由 HM2014nuli 于 2014-10-12 07:49 编辑

楼主你好,完全没发生问题,之所以第2个b的值那么长是因为float值的原因,还有不是字符转整数,是对char类型的值进行类型提升char ch='s';
System.out.println(ch+2);打印的是字符s对应的值再加上2,结果是117

回复 使用道具 举报
run_wind 发表于 2014-10-12 07:28
输出怎么是双引号+变量,和别的语言不太一样啊。我刚看到数据转换这里迫不及待的敲了代码,可能没看到这 ...
  1. /**
  2. 此程序用来诠释数据类型转换
  3. 思路:自动类型转换例子: 短整型转换成整型   单精度转换成双精度  字符型转换成整型
  4.       强制类型转换例子: 长整型转换成整型  整形转换成字节   整数转换成字符输出

  5. 步骤:
  6. 1.定义一个类
  7. 2.主函数
  8. 3.声明数据类型,赋值
  9. 4.转换并输出到屏幕
  10. */
  11. class Transformation
  12. {
  13.         public static void main(String args[])
  14.         {
  15.         int x=4;
  16.         short y=3;
  17.         x=x+y;//整数=整数+短整数
  18.         System.out.println("x="+x);
  19.     float a=3.1f;
  20.         double b=4.3;
  21.         b=b+a;//双精度小数=双精度小数+单精度小数
  22.         System.out.println("b="+b);
  23.         System.out.println('v'+2);//ASCLL码,字符转换成整数输出
  24.         long m=6;
  25.         int n=3;
  26.         byte q=1;
  27.         n=(int)(m+n);
  28.         System.out.println("n="+n);//长整型转换成整型
  29.         System.out.println("n="+(byte)(n+q));//整形转换成字节
  30.         System.out.println((char)5);//整数5强制转换为字符
  31.         System.out.println((int)b);//小数取整,实际上就是强制转换的过程
  32.         }
  33. }
复制代码
回复 使用道具 举报
HM2014nuli 发表于 2014-10-12 07:44
楼主你好,完全没发生问题,之所以第2个b的值那么长是因为float值的原因,还有不是字符转整数,是对char类 ...
  1. /**
  2. 此程序用来诠释数据类型转换
  3. 思路:自动类型转换例子: 短整型转换成整型   单精度转换成双精度  字符型转换成整型
  4.       强制类型转换例子: 长整型转换成整型  整形转换成字节   整数转换成字符输出

  5. 步骤:
  6. 1.定义一个类
  7. 2.主函数
  8. 3.声明数据类型,赋值
  9. 4.转换并输出到屏幕
  10. */
  11. class Transformation
  12. {
  13.         public static void main(String args[])
  14.         {
  15.         int x=4;
  16.         short y=3;
  17.         x=x+y;//整数=整数+短整数
  18.         System.out.println("x="+x);
  19.     float a=3.1f;
  20.         double b=4.3;
  21.         b=b+a;//双精度小数=双精度小数+单精度小数
  22.         System.out.println("b="+b);
  23.         System.out.println("b="+(float)b);
  24.         System.out.println('v'+2);//ASCLL码,字符转换成整数输出
  25.         long m=6;
  26.         int n=3;
  27.         byte q=1;
  28.         n=(int)(m+n);
  29.         System.out.println("n="+n);//长整型转换成整型
  30.         System.out.println("n="+(byte)(n+q));//整形转换成字节
  31.         System.out.println((char)5);//整数5强制转换为字符
  32.         System.out.println((int)b);//小数取整,实际上就是强制转换的过程
  33.         }
  34. }
复制代码
回复 使用道具 举报
问题很好,基础也没问题。问题是不够细心。
回复 使用道具 举报
jtshawn 中级黑马 2014-10-12 10:35:19
8#
lz太粗心了吧,好多错误都是不细心造成的
回复 使用道具 举报
出的问题都是粗心的建议去复习一下java的基本语法吧
改了一下。
  1. class Transformation
  2. {
  3.         public static void main(String args[])
  4.         {
  5.                 int x=4;
  6.                 short y=3;
  7.                 x=x+y;
  8.                 System.out.println("x="+x);        // 连接字符串用+,并且=放在双引号内。下面几个也是。
  9.                 float a=3.1f;        // 这里在3.1后面加个f,表示float类型的小数。
  10.                 double b=4.3;
  11.                 b=b+a;
  12.                 System.out.println("b="+b);
  13.                 System.out.println('v'+2);
  14.                 long m=6;
  15.                 int n=3;
  16.                 byte b1=1;                        // 这里的b改个名字,因为上面已经定义过了b,为double类型。
  17.                 n=(int)(m+n);
  18.                 System.out.println("n="+n);
  19.                 System.out.println("n="+(byte)(n+b1));
  20.         }
  21. }
复制代码



回复 使用道具 举报
你不是用eclipse写的代码吧。。可以用一下方便查找错误
回复 使用道具 举报
面具猴 发表于 2014-10-12 13:03
你不是用eclipse写的代码吧。。可以用一下方便查找错误

学习初期还是不用高级编译器的好
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马